开发者

Is there a programmatic way to determine whether the file is being used?

Case and point: I need to open an Excel file via Interop and it would help a lot to avoid ugly COM errors if I knew that the file was in use.

Other than trying to open the file and catching the exception, is there a programmatic way to determine whether the file is in use开发者_如何学运维?


You will need to use the Win32 API fileopen (CreateFile or lopen) for exclusive access and check the return value.


Whatever method you use, Nothing garanties that between your call and actually opening the File that another process hasn't opened the file.

Some pseudo code:

internal File OpenExcelFile(String fileName)
{
    File file = null;
    var fileOpened = SomeLibrary.IsFileOpened(fileName);

    if (!fileOpened)
    {
       // Nothing garanties that another process didnt grabbed the file between call and that the file is still closed!!!
       file = ExcelLibrary.OpenFile(fileName);
    }

    return file;
}


Its worse then that. Not only do you need to catch an exception, you have to use reflection to disambiguate it from other errors. At least, thats the only solution I found.

        try
        {
            using (StreamWriter sw = new StreamWriter(filepath, false))
            {
                sw.Write(contents);
            }
        }
        catch (System.IO.IOException exception)
        {
            if (!FileUtil.IsExceptionSharingViolation(exception))
                throw;
        }

...

    public static bool IsExceptionSharingViolation(IOException exception)
    {
        Type type = typeof(Exception);

        PropertyInfo pinfo = type.GetProperty("HResult", BindingFlags.NonPublic | BindingFlags.Instance);

        uint hresult = (uint)(int)pinfo.GetValue(exception, null);

        //ERROR_SHARING_VIOLATION = 32
        //being an HRESULT adds the 0x8007

        return hresult == 0x80070020;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜