简体   繁体   English

文件正在由另一个进程C#visual studio使用

[英]File is being used by another process C# visual studio

I have created a (Web application) web scheduler to call a method every 5min in global.asax file. 我创建了一个(Web应用程序)Web调度程序,以每5分钟在global.asax文件中调用一个方法。 It is getting triggered successfully. 它已成功触发。

In the method I am creating excel using interop dll and doing 'save as' as operation. 在该方法中,我使用interop dll创建excel并执行“另存为”操作。 First I am checking if file exists in the folder, if exists I am deleting it and doing "save as" operation. 首先,我要检查文件夹中是否存在文件,如果存在,则将其删除并执行“另存为”操作。 For the first time it works fine. 第一次可以正常工作。 For second call when the file exists I am not able to delete it. 对于第二个呼叫,当文件存在时,我无法将其删除。 It says file is being used by another process. 它说文件正在被另一个进程使用。

Once the debugger is stopped and run again I am able to delete the file, that too it only works for the first time. 一旦调试器停止并再次运行,我就可以删除该文件,它也只能在第一次使用。

I think visual studio (Asp.net development server) is locking the file. 我认为Visual Studio(Asp.net开发服务器)正在锁定文件。 once the development server is stopped I am able to delete it. 开发服务器停止后,便可以将其删除。

Please find the code below:- 请在下面找到代码:

 string fullFileName = Path.Combine(Path.GetTempPath(), testf); FileInfo TheFileInfo = new FileInfo(filePath); if (TheFileInfo.Exists) { File.Delete(fullFileName); } xlWorkBook.SaveAs(fullFileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue,misValue); //xlApp = new Microsoft.Office.Interop.Excel.ApplicationClass(); GC.Collect(); GC.WaitForPendingFinalizers(); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); xlApp.Application.Quit(); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(chartRange); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkSheet); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlWorkBook); System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp); 

Please help to fix this issue. 请帮助解决此问题。

You must have created object to access excel file. 您必须已创建对象才能访问excel文件。 Try closing that object strictly. 尝试严格关闭该对象。 If that object is not disposed then you'll get this error. 如果未处理该对象,则将收到此错误。 Excel objects are heavy. Excel对象很重。

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment. Microsoft当前不建议也不支持任何无人参与的非交互客户端应用程序或组件(包括ASP,ASP.NET,DCOM和NT Services)中的Microsoft Office应用程序自动化,因为Office可能表现出不稳定的行为和/在此环境中运行Office时出现死锁或死锁。

If you are building a solution that runs in a server-side context, you should try to use components that have been made safe for unattended execution. 如果要构建在服务器端上下文中运行的解决方案,则应尝试使用对无人值守执行安全的组件。 Or, you should try to find alternatives that allow at least part of the code to run client-side. 或者,您应该尝试找到允许至少部分代码在客户端运行的替代方法。 If you use an Office application from a server-side solution, the application will lack many of the necessary capabilities to run successfully. 如果您从服务器端解决方案中使用Office应用程序,则该应用程序将缺少许多成功运行所需的功能。 Additionally, you will be taking risks with the stability of your overall solution. 此外,您将承担整体解决方案稳定性的风险。 Read more about that in the Considerations for server-side Automation of Office article. 在《 服务器端Office自动化注意事项》一文中了解有关此内容的更多信息。

You may consider using the Open XML SDK, see Welcome to the Open XML SDK 2.5 for Office for more information. 您可以考虑使用Open XML SDK,有关更多信息,请参阅欢迎使用Office Open XML SDK 2.5 Or any other third-party components designed for the server-side execution. 或设计用于服务器端执行的任何其他第三方组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM