C# 错误“无法复制文件,因为它正被另一个进程使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18321422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error "unable to copy file because it is being used by another process
提问by Hossein Hadavi
I use windowsform application using C# language, I have many forms, and when I want to traverse from one to another, I use this.Hide();
我使用windowsform应用程序,使用C#语言,我有很多表单,当我想从一个到另一个遍历时,我使用this.Hide();
When I use this method, I receive the shown error,
当我使用这种方法时,我收到了显示的错误,
I know that the solution is to end process using windows task manager, But the question is that Is there any way I can use travel between forms without leading to this error?
我知道解决方案是使用 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
采纳答案by bash.d
Although you Hide()
your forms, they still remain as a part of the process. If you don't close every form properly, the process will remain running and you cannot recompile your project and eventually you will have to kill your process using the taskmanager.
Visual Studio tries to rewrite your executable and if the executable runs as a process, Windows will refuse to write to your .exe.
尽管您Hide()
填写了表格,但它们仍然是流程的一部分。如果您没有正确关闭每个表单,该进程将继续运行并且您无法重新编译您的项目,最终您将不得不使用任务管理器终止您的进程。
Visual Studio 尝试重写您的可执行文件,如果该可执行文件作为进程运行,Windows 将拒绝写入您的 .exe。
回答by kbvishnu
I believe this is happen while you compiling the application or you start debugging the application.
我相信这是在您编译应用程序或开始调试应用程序时发生的。
This may be due to.
这可能是由于。
- While you stop the debug in VS, the process may not be stopped.
- May be you just executed the application, by double clicking it.
- Might be some issues due to permission. If you are using source control, some folders may be treated as read only while downloading the project. So you need to edit that manually.
- Make sure that you used Application.Exit() to exit from all the process. http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit.aspx
- 在 VS 中停止调试时,进程可能不会停止。
- 可能是您刚刚执行了该应用程序,双击它。
- 由于许可,可能存在一些问题。如果您使用源代码管理,则在下载项目时某些文件夹可能会被视为只读。所以你需要手动编辑它。
- 确保您使用 Application.Exit() 退出所有进程。 http://msdn.microsoft.com/en-us/library/system.windows.forms.application.exit.aspx
回答by Moshe Reubinoff
I managed to fix it by deleting the suo file (hidden solution file).
我设法通过删除 suo 文件(隐藏的解决方案文件)来修复它。
回答by Mayank
Easy going solution is to add some code in Project Properties..
简单的解决方案是在项目属性中添加一些代码..
Just go to Project tab and select project properties.. From there select Build event tab. And in Pre-build event Command line add the following code..
只需转到“项目”选项卡并选择项目属性。从那里选择“构建事件”选项卡。并在预构建事件命令行中添加以下代码..
if exist "$(TargetPath).locked" del "$(TargetPath).locked"
if not exist "$(TargetPath).locked" move "$(TargetPath)" "$(TargetPath).locked"
回答by chevhfghfghfgh
I finally how fix it. Why we can't continue debug after the first debug because the first debug exe still running. So that, after first debug, you need to go to Task Manager -> Process Tab -> [your project name exe] end the exe process.
我终于如何解决它。为什么我们在第一次调试后无法继续调试,因为第一个调试 exe 仍在运行。这样,在第一次调试后,您需要转到任务管理器 -> 进程选项卡 -> [您的项目名称 exe] 结束 exe 进程。
回答by Tzahi
Just delete the .exe
file in the bin
folder: bin\Debug\WindowsFormsApplication1.exe
只需删除.exe
文件bin
夹中的文件:bin\Debug\WindowsFormsApplication1.exe
Don't worry, the build will create a new one.
别担心,构建将创建一个新的。
回答by Inspiration
close visual studio in windows 7 click Shift + Ctrl + esc in Tab processes find process like the name of your project and end it.
在 Windows 7 中关闭 Visual Studio 在 Tab 进程中单击 Shift + Ctrl + esc 查找进程名称,如项目名称并结束它。
回答by Muhammad Saeed
Simple method is to delete the .exe file from the project directory folder and build the project again. this will create new exe file and problem will solve.
简单的方法是从项目目录文件夹中删除.exe文件,然后重新构建项目。这将创建新的 exe 文件,问题将解决。