How do i make .txt file watcher with VS

i was trying to make .txt file watcher and i just added these functions;
private FileSystemWatcher watcher;


public Form1()
{
InitializeComponent();
InitializeFileSystemWatcher();
}

private void InitializeFileSystemWatcher()
{
watcher = new FileSystemWatcher();
watcher.Path = Settings1.Default.settingsFolderPath; // İzlenecek dizin
watcher.Filter = "veriler.txt"; // İzlenecek dosyanın adı

// Değişiklikleri izleme özelliklerini ayarla
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += OnFileChanged;

// İzlemeyi başlat
watcher.EnableRaisingEvents = true;
}

private void OnFileChanged(object sender, FileSystemEventArgs e)
{
// Dosya değiştiğinde çağrılacak metot
// Listenin yenilenmesi işlemlerini burada gerçekleştirin
RefreshList();
}

private void RefreshList()
{
table.Rows.Clear();

string filePath = Path.Combine(Settings1.Default.settingsFolderPath, "veriler.txt");
if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] values = line.Split('|');

// Her bir değerin başındaki ve sonundaki boşlukları kırp
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}

// Yeni satır oluştur ve değerleri tabloya ekle
table.Rows.Add(values);
}
}
else
{
MessageBox.Show("Veriler.txt not found.");
}
}
private FileSystemWatcher watcher;


public Form1()
{
InitializeComponent();
InitializeFileSystemWatcher();
}

private void InitializeFileSystemWatcher()
{
watcher = new FileSystemWatcher();
watcher.Path = Settings1.Default.settingsFolderPath; // İzlenecek dizin
watcher.Filter = "veriler.txt"; // İzlenecek dosyanın adı

// Değişiklikleri izleme özelliklerini ayarla
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Changed += OnFileChanged;

// İzlemeyi başlat
watcher.EnableRaisingEvents = true;
}

private void OnFileChanged(object sender, FileSystemEventArgs e)
{
// Dosya değiştiğinde çağrılacak metot
// Listenin yenilenmesi işlemlerini burada gerçekleştirin
RefreshList();
}

private void RefreshList()
{
table.Rows.Clear();

string filePath = Path.Combine(Settings1.Default.settingsFolderPath, "veriler.txt");
if (File.Exists(filePath))
{
string[] lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] values = line.Split('|');

// Her bir değerin başındaki ve sonundaki boşlukları kırp
for (int i = 0; i < values.Length; i++)
{
values[i] = values[i].Trim();
}

// Yeni satır oluştur ve değerleri tabloya ekle
table.Rows.Add(values);
}
}
else
{
MessageBox.Show("Veriler.txt not found.");
}
}
but this RefreshList() code is too broken, my veriler.txt datas filled like this texts;
3 Replies
ThєSαvícs ↝ darKSaviC
Dark | Savic | APLİKASYON | 05343543435 | Yok | antakya | 1_Mıntıka | | 6 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:04:00
Dark | Savic | APLİKASYON | 05343543435 | Yok | antakya | 1_Mıntıka | | 6 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:04:00
and
Light | Savic | APLİKASYON | 05343583935 | Yok | antakya | 1_Mıntıka | 56 | 4 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:05:00
Light | Savic | APLİKASYON | 05343583935 | Yok | antakya | 1_Mıntıka | 56 | 4 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:05:00
in the first example eighth one is sometimes can be empty or not depending on how I fill them out, they are recorded in the "veriler.txt" file. If I summarize the code along with its errors; when there is an update in 'veriler.txt', my DataGridView table named 'veriTablosu' is deleted and reloaded. However, when this table is deleted and reloaded, as shown in the example above, the 8th and empty data shift to the left, and the 9th, 10th, 11th, 12th, and 13th data take their place. Also, there is a code assigned to Cell_Click where data is sent to a total of 10 TextBoxes and 2 ComboBoxes located at the top whenever a cell is clicked. Somehow, this code also transfers the spaces around the '|' symbol in 'veriler.txt' to the 'veriTablosu' along with the data. As a result, spaces are added before and after the text in the total of 10 TextBoxes, and nothing is selected in the ComboBoxes as if that data were not there. . Thats why i need to help its too complicated for me
lycian
lycian8mo ago
I don't really understand. What are you visibly seeing? Just doing a simple repro I'm getting something easy to use:
class Form1 : Form
{
private DataGridView dgView = new DataGridView();
private static readonly string[] s_data = new[]
{
@"Dark | Savic | APLİKASYON | 05343543435 | Yok | antakya | 1_Mıntıka | | 6 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:04:00",
@"Light | Savic | APLİKASYON | 05343583935 | Yok | antakya | 1_Mıntıka | 56 | 4 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:05:00"
};

public Form1()
{
SetupLayout();
PopulateData();
}

private void PopulateData()
{
dgView.RowHeadersVisible = false;

dgView.ColumnCount = s_data[0].Split("|").Length;

for (var i = 0; i < dgView.ColumnCount; i++)
{
dgView.Columns[i].Name = i.ToString();
}

foreach (var s in s_data)
{
var values = s.Split("|").Select(v => v.Trim());
dgView.Rows.Add(values.ToArray());
}
}

private void SetupLayout()
{
this.Size = new Size(600, 500);

var panel = new Panel();
panel.Dock = DockStyle.Fill;
panel.Controls.Add(dgView);
dgView.Dock = DockStyle.Fill;

this.Controls.Add(panel);
}
}
class Form1 : Form
{
private DataGridView dgView = new DataGridView();
private static readonly string[] s_data = new[]
{
@"Dark | Savic | APLİKASYON | 05343543435 | Yok | antakya | 1_Mıntıka | | 6 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:04:00",
@"Light | Savic | APLİKASYON | 05343583935 | Yok | antakya | 1_Mıntıka | 56 | 4 | 5000 | 4000 | ARAZİ YAPILMADI | 13.04.2024 19:05:00"
};

public Form1()
{
SetupLayout();
PopulateData();
}

private void PopulateData()
{
dgView.RowHeadersVisible = false;

dgView.ColumnCount = s_data[0].Split("|").Length;

for (var i = 0; i < dgView.ColumnCount; i++)
{
dgView.Columns[i].Name = i.ToString();
}

foreach (var s in s_data)
{
var values = s.Split("|").Select(v => v.Trim());
dgView.Rows.Add(values.ToArray());
}
}

private void SetupLayout()
{
this.Size = new Size(600, 500);

var panel = new Panel();
panel.Dock = DockStyle.Fill;
panel.Controls.Add(dgView);
dgView.Dock = DockStyle.Fill;

this.Controls.Add(panel);
}
}
No description
lycian
lycian8mo ago
s_data was just copied from what you had here
Want results from more Discord servers?
Add your server