错误"无法复制文件,因为它正被另一个进程使用

Hos*_*avi 7 c# forms visual-studio-2010 winforms

我使用C#语言的windowsform应用程序,我有很多表单,当我想从一个遍历到另一个时,我使用this.Hide();

当我使用此方法时,我收到显示的错误,

我知道解决方案是使用Windows任务管理器结束进程,但问题是,有没有办法在表单之间使用旅行而不会导致此错误?

 Error  9   Unable to copy file "obj\x86\Debug\WindowsFormsApplication1.exe" to "bin\Debug\WindowsFormsApplication1.exe". The process cannot access the file 'bin\Debug\WindowsFormsApplication1.exe' because it is being used by another process.  C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets  2868
Run Code Online (Sandbox Code Playgroud)

小智 12

简单的解决方案是在Project Properties中添加一些代码.

只需转到Project选项卡并选择项目属性.从那里选择Build event选项卡.并在预构建事件命令行中添加以下代码..

   if exist "$(TargetPath).locked" del "$(TargetPath).locked"
   if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
Run Code Online (Sandbox Code Playgroud)


bas*_*h.d 7

虽然Hide()您是表单,但它们仍然是该过程的一部分.如果您没有正确关闭每个表单,该进程将继续运行,您无法重新编译项目,最终您将不得不使用taskmanager终止您的进程.
Visual Studio尝试重写您的可执行文件,如果可执行文件作为进程运行,Windows将拒绝写入您的.exe文件.


Tza*_*ahi 6

只需删除文件夹.exe中的文件即可binbin\Debug\WindowsFormsApplication1.exe

别担心,构建会创建一个新的。