1

Below is the part code I currently use to write to a file,

Try
    Using sWriter As 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

Rajeev
  • 4,571
  • 2
  • 22
  • 35
  • You can use FileInfo class to check on the permissions of the file. But again, if some other process is using the file, that also could cost you an exception. – Zenwalker Jun 09 '11 at 11:01

2 Answers2

0

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.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
  • Yep! Exception Handling would remain there I was just looking for the way to get out of procedure if the file is being used. – Rajeev Jun 09 '11 at 11:26
0

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.

Community
  • 1
  • 1
Rev
  • 2,269
  • 8
  • 45
  • 75