I'm trying to do a Watcher to look if a new file is created in a folder, and if it happens, this file will be copied locally to my computer. What I'm trying to do is that Each time a new file is created in the folder, the program create a copy of that file to a folder in my computer and when the copy were completed, it delete the original file.
I was using a few methods and all of them work fine, but my team asked me to make possible to change the destination folder and the origin folder.
When the program is in this step and I change one path (origin or destination), it works, but if I change both, this message is displayed:
System.UnauthorizedAccessException: 'Access to the path '\MXD6C4Y7M2\Users\dmontane\Desktop\test2\paco.xml' is denied.'
This issue occurs sometimes with File.Copy and sometimes with File.Delete. I think that the Watcher is being implemented in the wrong way. Plese advise me, my code is below.
The path value is from the extern folder and the path2 value is my computer folder. after the copy is done, the original folder is deleted but I want to make the program to look if the copy was made successfully and if it was, it's then deleted it.
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
FileSystemWatcher watcher = new FileSystemWatcher();
FileSystemWatcher watcher2 = new FileSystemWatcher();
try
{
watcher.Path = pathr;
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.xml*";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
}
catch (Exception e)
{
MessageBox.Show("Error "+e.Message);
}
try
{
watcher2.Path = path2;
watcher2.NotifyFilter = NotifyFilters.LastWrite;
watcher2.Filter = "*.xml*";
watcher2.Changed += new FileSystemEventHandler(LocalChange);
watcher2.EnableRaisingEvents = true;
}
catch (Exception e)
{
MessageBox.Show("Error " + e.Message);
}
}
// -----------------------------------------Define the event handlers---------------------------------------------------------------------------------------
private static void OnChanged(object source, FileSystemEventArgs e)
{
//especifica que se hace cuando un archivo se cambia crea o se borra
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
sourceFile = Path.Combine(pathr, fileName);
destFile = Path.Combine(path2, fileName);
if (!Directory.Exists(path2))
{
Directory.CreateDirectory(path2);
}
try
{
File.Copy(sourceFile, destFile, true);
}
catch(IOException ex)
{
}
cambio = true;
}
private static void LocalChange(object source, FileSystemEventArgs e)
{
//especifica que se hace cuando un archivo se cambia crea o se borra
sourceFile = Path.Combine(pathr, fileName);
destFile = Path.Combine(path2, fileName);
if (File.Exists(sourceFile))
{
try
{
File.Delete(sourceFile);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
}
cambio = true;
}
And this is the button code to change the path
private void button4_Click(object sender, EventArgs e)
{
path2 = textBox10.Text;
pathr = textBox11.Text;
Run();
try
{
t.DeserializeObject(path2 + @"\paco.xml");
textBox1.Text = t.Station;
textBox2.Text = t.SerialNumber;
textBox3.Text = t.PartNumber;
textBox4.Text = t.Description;
textBox5.Text = t.Quantity.ToString();
textBox6.Text = t.Customer;
textBox7.Text = t.LotNumber.ToString();
textBox8.Text = t.QADLine;
}
catch (Exception w)
{
textBox1.Text = "no se encuentra paco.xml";
textBox2.Text = "no se encuentra paco.xml";
textBox3.Text = "no se encuentra paco.xml";
textBox4.Text = "no se encuentra paco.xml";
textBox5.Text = "no se encuentra paco.xml";
textBox6.Text = "no se encuentra paco.xml";
textBox7.Text = "no se encuentra paco.xml";
textBox8.Text = "no se encuentra paco.xml";
}
if (textBox1.Text == "no se encuentra paco.xml")
{
MessageBox.Show(@"No se encuentra el archivo paco.xml intenta con otra carpeta","Error en direccion", MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}