Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#解锁目录并复制文件_C# - Fatal编程技术网

C#解锁目录并复制文件

C#解锁目录并复制文件,c#,C#,我必须用应用程序exe文件删除目录。 看起来是这样的: Directory.SetCurrentDirectory(@"C:\User\Temp"); 从C:\Folder\App.exe启动App.exe App.exe正在将自身复制到C:\User\Temp\Temp.exe App.exe正在关闭自身并运行Temp.exe Temp.exe正在删除App.exe和C:\文件夹目录 它看起来不错,但当我将App.exe复制到Temp.exe时,process Temp.exe仍在使用C:\

我必须用应用程序exe文件删除目录。 看起来是这样的:

Directory.SetCurrentDirectory(@"C:\User\Temp");
  • 从C:\Folder\App.exe启动App.exe
  • App.exe正在将自身复制到C:\User\Temp\Temp.exe
  • App.exe正在关闭自身并运行Temp.exe
  • Temp.exe正在删除App.exe和C:\文件夹目录
  • 它看起来不错,但当我将App.exe复制到Temp.exe时,process Temp.exe仍在使用C:\Folder。 无论我做什么,我启动的每个进程都会锁定我的目录

    我制作了一个表单应用程序,通过点击按钮来检查他们的行为

        bool del = false;
        public Form1(string[] args)
        {
            InitializeComponent();
            this.Text = Process.GetCurrentProcess().Id.ToString();
            if (args.Length > 0 && args[0] == "arg1")
            {
                Process proc = Process.GetProcessById(Convert.ToInt32(args[1]));
                proc.Kill();
            }
            else if (args.Length > 0 && args[0] == "arg2")
            {
                del = true;
            }
            else
            {
                string tempfile = Environment.GetEnvironmentVariable("TEMP") + "\\Temp.exe";
                File.Copy(Application.ExecutablePath, tempfile, true);
                Process proc = new Process();
                proc.StartInfo.FileName = tempfile;
                proc.StartInfo.Arguments = String.Format("arg1 {0}", Process.GetCurrentProcess().Id);
                proc.Start();
                Application.Exit();
            }
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (del == true)
            {
                string ApplicationPath = @"C:\Folder";
                DirectoryInfo directory = new DirectoryInfo(ApplicationPath);
                foreach (FileInfo file in directory.GetFiles()) file.Delete();
                Directory.Delete(ApplicationPath);
            }
            else
            {
                ProcessStartInfo Info = new ProcessStartInfo();
                Info.Arguments = "/C ping 127.0.0.1 -n 2 && \"" + Application.ExecutablePath + "\" arg2";
                Info.WindowStyle = ProcessWindowStyle.Hidden;
                Info.CreateNoWindow = true;
                Info.FileName = "cmd.exe";
                Process.Start(Info);
            }
        }
    
    简而言之,我正在寻找一种解决方案,可以删除父目录下的启动exe文件


    希望得到帮助。谢谢。

    我怀疑问题在于应用程序仍然从当前目录运行,即使它运行的是单独的可执行文件。例如,考虑命令行:

    C:\SomeFolder>../AnotherFolder/SomeProgram.exe
    
    虽然
    SomeProgram
    可能在另一个文件夹中,但我自己“在”
    SomeFolder
    中,因此保持对它的引用打开。所以它不能被删除

    不过,您应该能够从代码中删除。大概是这样的:

    Directory.SetCurrentDirectory(@"C:\User\Temp");
    

    我怀疑问题在于应用程序仍然从当前目录运行,即使它运行的是单独的可执行文件。例如,考虑命令行:

    C:\SomeFolder>../AnotherFolder/SomeProgram.exe
    
    虽然
    SomeProgram
    可能在另一个文件夹中,但我自己“在”
    SomeFolder
    中,因此保持对它的引用打开。所以它不能被删除

    不过,您应该能够从代码中删除。大概是这样的:

    Directory.SetCurrentDirectory(@"C:\User\Temp");
    

    主要是猜测,但可能需要先更改当前工作目录?在按钮上单击事件处理程序,如果del==true。。。在if语句中,检查文件夹中的文件是否不在use@David谢谢我已经更改了工作目录,现在一切都好了。@user5817386:太好了!我将在下面给出一个答案,这样将来的用户可能会更容易找到它。稍等片刻……基本上是猜测,但也许您需要先更改当前工作目录?在按钮上单击事件处理程序,如果del==true。。。在if语句中,检查文件夹中的文件是否不在use@David谢谢我已经更改了工作目录,现在一切都好了。@user5817386:太好了!我将在下面给出一个答案,这样将来的用户可能会更容易找到它。等一下。。。