为的errorCode进程无法访问文件“XYZ”,因为它正由另一个进程使用 [英] errorCode for The process cannot access the file 'XYZ' because it is being used by another process
问题描述
我使用C#.NET,Visual Studio 2008,.NET 3.5
I using C# .NET , vs 2008 , .net 3.5
对于我来说,是困难的,但我需要这在C#示例代码:
For me, is difficult, but I need sample code in C# for this:
- 如何获得IOException异常的错误代码该进程无法访问文件XYZ,因为它正由另一个进程使用。
例如,在我的问题。
我试图删除文件,我得到该进程无法访问文件XYZ,因为它正由另一个进程使用。 。异常
I try delete file, and I get "The process cannot access the file 'XYZ' because it is being used by another process." Exception.
try
{
File.Delete(infoFichero.Ruta);
}
catch (IOException ex)
{
// ex.Message == "The process cannot access the file 'XYZ' because it is being used by another process."
}
但是,如果.NET是西班牙人,我得到厄尔尼诺proceso没有puede obtener acceso人archivo00000004.PDFporque ESTA siendo utilizado连接OTRO proceso的消息。
But if .NET is Spanish, I get "El proceso no puede obtener acceso al archivo '00000004.PDF' porque está siendo utilizado en otro proceso" message.
System.IO.IOException: El proceso no puede obtener acceso al archivo '00000004.PDF' porque está siendo utilizado en otro proceso.
en System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
en System.IO.FileInfo.Delete()
我需要为异常的错误代码。在跟踪,我看到System.IO .__ Error.WinIOError(的Int32的errorCode,字符串maybeFullPath)
I need a ERROR CODE for that Exception. In Trace, I have seen System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
如何获得IOException的过程中的错误代码无法访问该文件' XYZ',因为它正由另一个进程使用。
How get the error code of IOException "The process cannot access the file 'XYZ' because it is being used by another process."
推荐答案
您可能已经注意到,产生HResult属性是不可访问。解决方法是使用Marshal.GetLastWin32Error()方法来获取本地Windows错误代码。像这样的:
You might have noticed that the HResult property is not accessible. The workaround is to use the Marshal.GetLastWin32Error() method to get the native Windows error code. Like this:
catch (IOException ex) {
int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
if (err == 32) Console.WriteLine("It's locked");
// etc..
}
错误代码32名为ERROR_SHARING_VIOLATION在该SDK。
Error code 32 is named ERROR_SHARING_VIOLATION in the SDK.
这篇关于为的errorCode进程无法访问文件“XYZ”,因为它正由另一个进程使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!