I made a method that writes a txt file on a usb flash drive. At the moment I start that method via a RelayCommand that is binded to a button. But I want to start that method evertime a usb flash drive is plugged in automatically. So that this method writes the txt-file automatically on the flash drive that is plugged in. (The method GetDrives replys me the driveletter of the first drive that is plugged in). Does anyone know how to realize that?
public void WriteFileOnFDrive()
{
var firstDrive = GetDrives().FirstOrDefault();
if (!string.IsNullOrEmpty(firstDrive))
{
string myText = "TestText";
string path = firstDrive + @"SomeText.txt";
if (File.Exists(path))
{
Console.WriteLine("SomeText already exists!");
}
else
{
System.IO.File.WriteAllText(path, myText);
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Hidden | FileAttributes.ReadOnly | FileAttributes.System);
}
}
}
Edit: I don´t need the drives that are already plugged in. I need to trigger my method, when a new usb flash drive gets plugged in.