将文件从可变文件夹移动到另一个 [英] Moving file from a varible folder to another

查看:85
本文介绍了将文件从可变文件夹移动到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用FileSystemWatcher来检测何时在spesifict文件夹中创建文件.

然后将文件发送到另一个文件夹.

如果该文件已经存在于另一个文件夹中,则应为其赋予版本号

例如:Test.txt是在C:\ Temp中创建的-FileSystemWatcher会检测到该文件,然后将其发送到-C:\ Temp2如果该文件已经存在于C:\ Temp2中,则应给该文件一个版本号,例如Test01. txt

我是C#的新手
我的主要问题是我不知道如何更改文件夹.

希望你们中的一些可以帮助我.

谢谢

代码:

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:

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());
            }
        }

推荐答案

第一个问题
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;
}


这篇关于将文件从可变文件夹移动到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆