Is there a safe way of detecting File access permissions
Below is the part code I currently use to write to a file,
Try
Using sWriter 开发者_StackOverflowAs New IO.StreamWriter("C:\Tmp.txt", False)
sWriter.Write(m_Buffer.ToString) : sWriter.Flush()
End Using
Return True
Catch ex As IOException
End Try
but some time this results in error
Access to the path 'C:\Tmp.txt' is denied
My question is, Is there a safe way of detecting File access permissions, without handling exceptions, I mean if I can some how check before opening a file for any filehandle opened for it.
I think I have made my point. Thanks in Advance
There have been other similar questions asked in StackoverFlow. check this before you ask!
How do you check for permissions to write to a directory or file?
how can you easily check if access is denied for a file in .NET?
The first link can help you directly.
Given that it's possible for a file to become locked after a hypothetical call to an IsFileLocked()
method, you are always going to want exception handling around your subsequent I/O operation.
Such code would like like what you have now.
精彩评论