Hi, David
How about this issue now?
For checking a file is in use or not, a better approach would be to call
the CreateFile API directly.
If the CreateFile API executs successfully, it will return an open handle
to the specified file, device, named pipe, or mail slot, we can then create
a FileStream object from this hadnle.
If the execution of CreateFile API fails, we can call the
Marshal.GetLastWin32Error() method to get the last error code, if the last
error code is ERROR_SHARING_VIOLATION , it means the file we check is in
use.
A sample code for your information
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
private static extern SafeFileHandle CreateFile(string lpFileName,
FileSystemRights dwDesiredAccess, FileShare dwShareMode, IntPtr
securityAttrs, FileMode dwCreationDisposition, FileOptions
dwFlagsAndAttributes, IntPtr hTemplateFile);
const int ERROR_SHARING_VIOLATION = 32;
private FileStream OpenFileStream(string fileName)
{
while (true)
{
try
{
SafeFileHandle fileHandle = CreateFile(fileName,
FileSystemRights.Modify, FileShare.Write,
IntPtr.Zero, FileMode.OpenOrCreate,
FileOptions.None, IntPtr.Zero);
if (!fileHandle.IsInvalid)
{
FileStream fs = new FileStream(fileHandle,
FileAccess.ReadWrite);
return fs;
}
}
catch (IOException ex)
{
//Do something with the exception, which usually should
not occur.
//Probably re-throw the exception.
}
int lastError = Marshal.GetLastWin32Error();
if (lastError != ERROR_SHARING_VIOLATION)
{
//The last error is not about sharing violation.
//Something unexpected may have occured.
}
Thread.Sleep(2000);
}
}
Through this approach, we can avoid the expensive exception handling.
Please try these and let me know how things going.
Best Regards
Zhi-Xin Ye
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.