0

I have below code which moves files for me with a timer (every 10 minutes 200 files are moved or what i save on my settings Form).

The problem is that files get moved when they are opened and written to, I know I have to "open" it in my program and check for an exception but I can't figure out where in my code I should put it?

try
{
            string origDir = @"" + Properties.Settings.Default.MoveFrom;
            string destDir = @"" + Properties.Settings.Default.MoveTo;
            string oldDir = @"" + Properties.Settings.Default.MoveToOld;

            int filesPerMove = Properties.Settings.Default.FilesPerMove;
            int i = 0;

            DateTime dateMove= DateTime.Now;

            DirectoryInfo dirInfo = new DirectoryInfo(origDir);
            FileInfo[] files = dirInfo.GetFiles("*.txt");

            foreach (string file in Directory.GetFiles(origDir))
                if (new FileInfo(file).Length > 0)
                {
                    if (i < filesPerMove)
                    {
                            ListViewItem lviSuccess = new ListViewItem(origFile.Name);
                            lviSuccess.SubItems.Add(origFile.LastWriteTime.ToString());
                            lviSuccess.SubItems.Add(dateMove.ToString());
                            lviSuccess.SubItems.Add(origFile.Length.ToString());
                            lviSuccess.SubItems.Add(origDir.ToString());
                            lviSuccess.SubItems.Add(destDir.ToString());
                            lvFileMoves.Items.Add(lviSuccess);
                            lviSuccess.UseItemStyleForSubItems = true;
                            lviSuccess.ForeColor = Color.Green;

                            FileInfo destFile = new FileInfo(file.Replace(origDir, destDir));
                            FileInfo destFile1 = new FileInfo(file.Replace(origDir, oldDir));
                            System.IO.File.Copy(origFile.FullName, destFile1.FullName, true);
                            System.IO.File.Copy(origFile.FullName, destFile.FullName, true);
                            File.Delete(origFile.FullName);                        
                            i++;
                        }
                    }
}
catch (System.IO.IOException ex)
{ 
            ListViewItem lvi = new ListViewItem(ex.Message);
            lvFileMoves.Items.Add(lvi);
            lvi.UseItemStyleForSubItems = true;
            lvi.ForeColor = Color.Red;
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

3 Answers3

1

Polling is Evil.

What you *really want is to be notified if a change occurs.

ReadDirectoryChangesW is one way to do this.

Here's a good link showing how to use it (from C++, translating to C#/Interop should be straightforward):

paulsm4
  • 114,292
  • 17
  • 138
  • 190
0

You should place the exception handling close to the file handling:

try
{
    FileInfo destFile = new FileInfo(file.Replace(origDir, destDir));
    FileInfo destFile1 = new FileInfo(file.Replace(origDir, oldDir));
    System.IO.File.Copy(origFile.FullName, destFile1.FullName, true);
    System.IO.File.Copy(origFile.FullName, destFile.FullName, true);
    File.Delete(origFile.FullName);                        
    i++;
}
catch(System.IO.IOException ex)
{ 
    ListViewItem lvi = new ListViewItem(ex.Message);
    lvFileMoves.Items.Add(lvi);
    lvi.UseItemStyleForSubItems = true;
    lvi.ForeColor = Color.Red;
}

This way you can continue with the following files when one file fails.

Casperah
  • 4,504
  • 1
  • 19
  • 13
  • Problem is still the same, that my program move the file i have opened in Notepad – user1538062 Oct 21 '12 at 18:51
  • Notepad does not lock the file while it is opened. There is no way to check if the file opened by notepad (or notepad2). But if it was opened by MS Word or OpenOffice Writer the file would be locked and you could check it. See http://stackoverflow.com/questions/11427265/is-there-a-way-to-programmatically-check-if-a-excel-file-is-opened for details – Casperah Oct 26 '12 at 18:03
0

Now i have below but it trows an exception for every file even for files which i haven't opened in Notepad.

 FileStream outStream = null;
 try
                    {
                        outStream = File.OpenWrite(origFile.ToString());

                            FileInfo destFile = new FileInfo(file.Replace(origDir, destDir));

                            FileInfo destFile1 = new FileInfo(file.Replace(origDir, oldDir));
                            System.IO.File.Copy(origFile.FullName, destFile1.FullName, true);
                            System.IO.File.Copy(origFile.FullName, destFile.FullName, true);
                            File.Delete(origFile.FullName);
                            lblSenast.Text = string.Format("Senaste flytt gjordes {0}",     dateMove.ToString());
                    }
                    catch (IOException exx)
                    {
                        ListViewItem lvi = new ListViewItem(exx.Message);
                        lvFileMoves.Items.Add(lvi);
                        lvi.UseItemStyleForSubItems = true;
                        lvi.ForeColor = Color.Gray;
                    }
                    finally
                    {
                        outStream.Close();
                        outStream.Dispose();
                    }

The process cannot access the file"Bla bla bla" Because it is being used by another process