Я студент и изучаю кодирование, но я действительно не могу понять, как это сделать. .err ) и в зависимости от расширения, делайте разные вещи. Я впервые использую формы Windows. Пересмотр. Когда я меняю расширение, он ничего не делает.using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Progetto01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string file;
private string directory;
private void chooseFile_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "C://Users//ut254//Desktop//Gabriele//Progetto01//Origine";
openFileDialog.Filter = "csv files (*.csv)|*.csv";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
file = openFileDialog.FileName;
MessageBox.Show(file);
}
}
}
private void chooseDir_Click(object sender, EventArgs e)
{
string dest = "C://Users//ut254//Desktop//";
string dest0 = "Gabriele//Progetto01";
using (FolderBrowserDialog openFileDialog = new FolderBrowserDialog())
{
openFileDialog.SelectedPath = Path.Combine(dest, dest0);
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
directory = openFileDialog.SelectedPath;
MessageBox.Show(directory);
}
}
}
private void start_Click(object sender, EventArgs e)
{
int cicle = 0;
bool yes;
FileSystemWatcher watcher = new FileSystemWatcher();
try
{
using (StreamReader toRead = new StreamReader(file))
{
string reader;
while ((reader = toRead.ReadLine()) != null)
{
yes = false;
if (cicle != 0)
{
string destPath = Path.Combine(directory, "data.csv");
string OK = Path.Combine(directory, "data.ok");
string ERR = Path.Combine(directory, "data.err");
watcher.Path = directory;
watcher.Filter = "data.csv";
File.WriteAllText(destPath, "");
File.WriteAllText(destPath, reader);
while (true)
{
Task.Run(() => Elabora(watcher, destPath, OK, ERR, reader, yes));
if (yes)
{
break;
}
}
}
cicle = 1;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Something went wrong: " + ex.Message);
}
}
private async Task Elabora(FileSystemWatcher watcher, string destPath, string ok, string err, string reader, bool yes)
{
await Task.Delay(500);
watcher.Renamed += (sender, e) =>
{
string newPath = e.FullPath;
if (Path.GetExtension(newPath) == ok)
{
File.Delete(newPath);
MessageBox.Show("The task was completed successfully!");
yes = true;
}
else if (Path.GetExtension(newPath) == err)
{
string newName = Path.Combine(Path.GetDirectoryName(newPath), "1234");
File.WriteAllText(newName, reader);
MessageBox.Show("Task failed.");
yes = true;
}
};
watcher.EnableRaisingEvents = true;
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... is-program