Click here to Skip to main content
16,015,414 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey

I`m using the FileSystemWatcher to detect when a file is created in a spesifict folder.

Then the file shall be sent to another folder.

If the file allready exists in the other folder, it shall be given a version number

ex: Test.txt is created in C:\Temp - FileSystemWatcher detects the file and then send the file to - C:\Temp2 IF the file allready exists in C:\Temp2 then the file shall be given a version number like Test01.txt

I`m new to C#
My main problem is that I don`t know how to change folder.

Hope some of you can help me.

Thanks

Code:

C#
void fileWatcher_EventCreated(object sender, WatcherExEventArgs e)
        {

            CreateListViewItem("Created", "N/A", (e.Arguments == null) ? "Null argument object" : ((FileSystemEventArgs)(e.Arguments)).FullPath);

            //THIS IS MY first problem, I want to be able to change folder. I want the folders to be read from the textboxs: textBoxFolderToWatch and textBoxtogo, and the file to be read from the FileSystemWatcher part
            string path = @"c:\temp\Test.txt";
            string path2 = @"c:\temp2\Test.txt";

            try
            {

                // THIS IS MY second problem, I want the new file to be  updated with a version number
                // Ensure that the target does not exist.

                if (File.Exists(path2))
                {

                    MessageBox.Show("Filen eksisterer!");
                    //File.Exists(path2) = true;

                    File.Move(path2, @"textBoxtogo.text\fileName1");
                    File.Move(path, path2);
                    //File.Delete(path2);


                }
                else
                {
                    // Move the file.
                    File.Move(path, path2);
                    Console.WriteLine("{0} was moved to {1}.", path, path2);
                }
                // See if the original exists now.
                if (File.Exists(path))
                {
                    MessageBox.Show("The original file still exists, which is unexpected.");
                }
                else
                {
                    MessageBox.Show("The original file no longer exists, which is expected.");
                }

            }
            catch (Exception error)
            {
                MessageBox.Show("The process failed: {0}", error.ToString());
            }
        }
Posted
Updated 21-Jun-10 2:36am
v3

1 solution

First problem
string path = Path.Combine(textBoxFolderToWatch.Text, FileName);
string path2 = Path.Combine(textBoxtogo.Text, FileName);

Very simple, even if you don't read the documentation

Second problem
private string GetPath(string path)
{
  int version = 1;
  string fileName = Path.GetFileNameWithoutExtension(path);
  string folder = Path.GetDirectoryName(path);
  string extentions = Path.GetExtention(path);2
  while( File.Exists(path) )
  {
     path = Path.Combine(folder, fileName + version++);
     path = Path.Combine(path, extention);
  }

  return path;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900