ekremakyuz
Femtopat
- Katılım
- 26 Mayıs 2023
- Mesajlar
- 48
Daha fazla
- Cinsiyet
- Erkek
C# ile host dosyanızı düzenlemenizi sağlayan yazılım.
Kodlarda aşağıda verilmiştir.
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace BlockSitesWithcsharp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string hostsPath = "C:\\WINDOWS\\system32\\drivers\\etc\\hosts";
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
string redirectDomain = "127.0.0.1";
void BlockAllSites()
{
try
{
using (StreamWriter sw = new StreamWriter(hostsPath))
{
foreach (var item in listBox1.Items)
{
string site = item.ToString();
sw.WriteLine(redirectDomain + "\t" + site);
}
}
}
catch (Exception)
{
}
}
void BlockSingle()
{
try
{
using (StreamWriter sw = new StreamWriter(hostsPath))
{
sw.WriteLine(redirectDomain + "\t" + listBox1.SelectedItem.ToString());
}
}
catch (Exception)
{
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
BlockAllSites();
}
private void button4_Click(object sender, EventArgs e)
{
File.Delete(@"C:\Windows\System32\drivers\etc\hosts");
Thread.Sleep(2000);
using (StreamWriter sw = new StreamWriter(hostsPath))
{
sw.WriteLine("# Hosts Cleaned time : " + DateTime.Now.ToString());
}
}
private void button2_Click(object sender, EventArgs e)
{
BlockSingle();
}
private void button5_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
Son düzenleme: