Host Dosyasını Düzenlemek c#

Merhaba arkadaşlar yine ufak bir program ile karşınızdayım.
 Bu program "C:/Windows/System32/drivers/etc/" dizininde bulunan hosts dosyasını düzenlemek için yazıldı. biliyorsunuz bazı programların server ile iletişimini kesmek için bu hosts dosyası düzenlenir ve yönlendirilecek dns adresi ve yönlenecek adres ayarlanır.

 Örneğin bilgisayarınızdan www.yandex.com.tr adresine girilmesini istemiyorsunuz bunun için hosts dosyasını açıp

 127.0.0.1 www.yandex.com.tr 

 satırını eklersiniz bilgisayar yandex.com.tr ye girmek yerine 127.0.0.1 (-> pcnizin localhost adresi) adresine sizi yönlendirecektir.
 Yada bilgisayarınızda iis server kurulu attınız dosyalarınızı servera adres çubuğuna localhost yada 127.0.0.1 yazmak yerine direk istediğiniz adresi yazın serhadbahadirtekkol.com.tr

127.0.0.1 serhadbahadirtekkol.com.tr

bu adres sizin localhostunuza yönlendirecektir.

lisanslı programları kullanırken sürekli karşımıza gelen yakalanma ekranından kurtulmanın bir yoluda budur.
buraya sistemin dns adreslerini girersiniz program server a veri gönderdiğini düşünür ancak sizin sisteminizdeki localhosta gönderir yada istediğiniz başka bir adrese.
Programın ekran görüntüsü aşağıdaki gibi
Programı Buradan indirebilirsiniz




programın kaynak kodları ise
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. ////////////-SerhadBahadir-////////
  2. ////Coded By SerhadBahadirTEKKOL
  3. ////bahadir_tekkol@hotmail.com
  4. //// 03.02.2014
  5. //////////////////////////////////
  6. ////Hosts File Edit Tool
  7. ///.Net Framework 4.5
  8. ///VS2013_Ultimate
  9. ////////////////////////////////
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.ComponentModel;
  14. using System.Data;
  15. using System.Drawing;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Windows.Forms;
  20. using System.IO;
  21.  
  22. namespace HostDuzenlemeProgram
  23. {
  24.  
  25.     public partial class Form1 : Form
  26.     {
  27.        
  28.                     string dosyayolu= "C:/Windows/System32/drivers/etc/hosts";
  29.      
  30.         public Form1()
  31.         {
  32.             InitializeComponent();
  33.  
  34.         }
  35.  
  36.         private void Form1_Load(object sender, EventArgs e)
  37.         {
  38.             toolTip1.SetToolTip(listBox1, " İşlemler İçin Right Click ");
  39.  
  40.  
  41.             using (StreamReader stRead = new StreamReader(dosyayolu))
  42.        
  43.                 while (!stRead.EndOfStream)
  44.                 {
  45.                     listBox1.Items.Add(stRead.ReadLine());
  46.                 }
  47.             int satirbasi = listBox1.Items.Count;
  48.             for (int i = 0; i <satirbasi; i++)
  49.             {
  50.                
  51.                 string cumle = listBox1.Items[i].ToString();
  52.                 if (cumle.StartsWith("#") || cumle.Length<1)
  53.                 {
  54.                     listBox1.Items.Remove(listBox1.Items[i]);
  55.                     i = i - 1;
  56.                     satirbasi = satirbasi - 1;
  57.                 }
  58.             }
  59.             }
  60.  
  61.  
  62.         private void button1_Click(object sender, EventArgs e)
  63.         {
  64.     string genellocal="";
  65.     if (textBox1.Text!="")
  66.     {
  67.      
  68.             if (textBox2.Text== "")
  69.             {
  70.                 genellocal = "127.0.0.1 ";
  71.             }
  72.             else
  73.             {
  74.                 genellocal=textBox2.Text+" ";
  75.  
  76.             }
  77.  
  78.             listBox1.Items[Convert.ToInt32(label3.Text)] =genellocal+ textBox1.Text; ;  
  79.     }
  80.     else
  81.     {
  82.         MessageBox.Show("Yönlendirilecek Dns ve adresi tam giriniz");
  83.     }
  84.          
  85.         }
  86.  
  87.        
  88.  
  89.         private string dosyaayristir(string adres)
  90.         {
  91.             string siralama = adres;
  92.            string sonuc;
  93.            string dnscozumlenen="";
  94.  
  95.            
  96.             int sirasi = siralama.IndexOf(" ");
  97.             if (sirasi!=-1)
  98.             {
  99.                 dnscozumlenen = siralama.Substring(0, sirasi);
  100.              sonuc = siralama.Remove(0, sirasi+1);
  101.    
  102.              label3.Text = listBox1.SelectedIndex.ToString();
  103.              if (sonuc.StartsWith(" ") || sonuc.StartsWith("\t"))
  104.              {
  105.                
  106.                  label3.Text =listBox1.SelectedIndex.ToString();
  107.              }
  108.             }
  109.             else
  110.  
  111.          
  112.             {
  113.                
  114.                 label3.Text = listBox1.SelectedIndex.ToString();
  115.                 sonuc = "";
  116.  
  117.             }
  118.            
  119.  
  120.             textBox2.Text=dnscozumlenen;
  121.                return sonuc;
  122.  
  123.         }
  124.  
  125.        
  126.  
  127.         private void linkLabel1_MouseClick(object sender, MouseEventArgs e)
  128.         {
  129.             textBox3.Text = listBox1.Items[Convert.ToInt32(label3.Text)].ToString();
  130.         }
  131.  
  132.         private void button2_Click(object sender, EventArgs e)
  133.         {
  134.             for (int i = 0; i < listBox1.Items.Count; i++)
  135.             {
  136.                 string cumle = listBox1.Items[i].ToString();
  137.                 if (cumle.StartsWith("127.0.0.1"))
  138.                 {
  139.  
  140.                     cumle = cumle.Remove(0, 9);
  141.                    cumle= cumle.Replace(" ", "");
  142.                    cumle = cumle.Replace("\t", "");
  143.                    cumle = "127.0.0.1 " + cumle;
  144.                     listBox1.Items[i] = cumle;
  145.                 }
  146.                 else
  147.                 {
  148.                     if (!cumle.StartsWith("::1"))
  149.                     {
  150.                         int ilkbos;
  151.                         string basdns;
  152.                         ilkbos = cumle.IndexOf(" ");
  153.                         basdns = cumle.Substring(0, ilkbos);
  154.                         cumle = cumle.Remove(0, ilkbos);
  155.                         cumle = cumle.Replace(" ", "");
  156.                         cumle = cumle.Replace("\t", "");
  157.                         cumle = basdns + " " + cumle;
  158.                         listBox1.Items[i] = cumle;
  159.                     }
  160.                    
  161.  
  162.  
  163.                 }
  164.              
  165.                
  166.             }
  167.         }
  168.  
  169.         private void tamamıToolStripMenuItem_Click(object sender, EventArgs e)
  170.         {
  171.             for (int i = 0; i < listBox1.Items.Count; i++)
  172.             {
  173.                 string cumle = listBox1.Items[i].ToString();
  174.                 if (cumle.StartsWith("127.0.0.1"))
  175.                 {
  176.  
  177.                     cumle = cumle.Remove(0, 9);
  178.                     cumle = cumle.Replace(" ", "");
  179.                     cumle = cumle.Replace("\t", "");
  180.                     cumle = "127.0.0.1 " + cumle;
  181.                     listBox1.Items[i] = cumle;
  182.                 }
  183.                 else
  184.                 {
  185.  
  186.                
  187.                     try
  188.                     {
  189.                         int ilkbos;
  190.                         string basdns;
  191.                         ilkbos = cumle.IndexOf(" ");
  192.                         basdns = cumle.Substring(0, ilkbos);
  193.                         cumle = cumle.Remove(0, ilkbos);
  194.                         cumle = cumle.Replace(" ", "");
  195.                         cumle = cumle.Replace("\t", "");
  196.                         cumle = basdns + " " + cumle;
  197.                         listBox1.Items[listBox1.SelectedIndex] = cumle;
  198.                     }
  199.                     catch
  200.                     {
  201.  
  202.                     }
  203.                    
  204.                    
  205.  
  206.                    
  207.  
  208.  
  209.                 }
  210.  
  211.  
  212.             }
  213.         }
  214.  
  215.         private void seçileniToolStripMenuItem_Click(object sender, EventArgs e)
  216.         {
  217.             if (listBox1.SelectedIndex==-1)
  218.             {
  219.                 MessageBox.Show("Öncelikle Seçim Yapınız");  
  220.             }
  221.             else
  222.             {
  223.                 string cumle = listBox1.Items[listBox1.SelectedIndex].ToString();
  224.                 if (cumle.StartsWith("127.0.0.1"))
  225.                 {
  226.  
  227.                     cumle = cumle.Remove(0, 9);
  228.                     cumle = cumle.Replace(" ", "");
  229.                     cumle = cumle.Replace("\t", "");
  230.                     cumle = "127.0.0.1 " + cumle;
  231.                     listBox1.Items[listBox1.SelectedIndex] = cumle;
  232.                 }
  233.                 else
  234.                 {
  235.                     int ilkbos;
  236.                     string basdns;
  237.                     ilkbos = cumle.IndexOf(" ");
  238.                     basdns = cumle.Substring(0, ilkbos);
  239.                     cumle = cumle.Remove(0, ilkbos);
  240.                     cumle = cumle.Replace(" ", "");
  241.                     cumle = cumle.Replace("\t", "");
  242.                     cumle = basdns+" " + cumle;
  243.                     listBox1.Items[listBox1.SelectedIndex] = cumle;
  244.  
  245.  
  246.                 }
  247.  
  248.  
  249.             }
  250.         }
  251.  
  252.         private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
  253.         {
  254.             textBox1.Text = dosyaayristir(listBox1.SelectedItem.ToString());
  255.         }
  256.  
  257.        
  258.  
  259.         private void duzenleToolStripMenuItem_Click(object sender, EventArgs e)
  260.         {
  261.             if (listBox1.SelectedIndex == -1)
  262.             {
  263.                 MessageBox.Show("Öncelikle Seçim Yapınız");
  264.             }
  265.             else
  266.             {
  267.                 textBox1.Text = dosyaayristir(listBox1.SelectedItem.ToString());
  268.             }
  269.         }
  270.  
  271.         private void button3_Click(object sender, EventArgs e)
  272.         {
  273.             for (int i = 0; i < header.Items.Count; i++)
  274.             {
  275.            listBox1.Items.Insert(i, header.Items[i]) ;  
  276.             }
  277.  
  278.             StreamWriter yazmaStream = new StreamWriter(dosyayolu);
  279.  
  280.             foreach (var item in listBox1.Items)
  281.             {
  282.                 yazmaStream.WriteLine(item.ToString());
  283.             }
  284.             yazmaStream.Close();
  285.             for (int i = 0; i < 19; i++)
  286.             {
  287.                 listBox1.Items.RemoveAt(0);
  288.             }
  289.         }
  290.  
  291.         private void button4_Click(object sender, EventArgs e)
  292.         {
  293.             if (textBox1.Text!="")
  294.             {
  295.                 string genellocal = "";
  296.                 if (textBox2.Text == "")
  297.                 {
  298.                     genellocal = "127.0.0.1 ";
  299.                 }
  300.                 else
  301.                 {
  302.                     genellocal = textBox2.Text + " ";
  303.  
  304.                 }
  305.  
  306.  
  307.                 listBox1.Items.Add(genellocal + " " + textBox1.Text);  
  308.             }
  309.            
  310.         }
  311.  
  312.         private void silToolStripMenuItem_Click(object sender, EventArgs e)
  313.         {
  314.             if (listBox1.SelectedIndex != -1)
  315.             {
  316.                 listBox1.Items.RemoveAt(listBox1.SelectedIndex);
  317.             }
  318.             else
  319.             {
  320.                 MessageBox.Show("silmek için lütfen listeden seçim yapınız");
  321.             }
  322.         }
  323.  
  324.      
  325.     }
  326. }

Yorumlar