Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checks if file can be written to? 1

Status
Not open for further replies.

jahchong

Programmer
Aug 10, 2003
18
0
0
MU
Hi

I have a text file that I use to hold some information. At one point, this text file needs to be edited. So far so good. The problem is that the file may be located on a media that does not allow write access (floppy with write protect, CD, or no write access on file for user).

I've tried opening the file with the streamreader and checked the basestream.canwrite but it always return false. And if i directly use streamwriter, when the file cannot be written to, I get the FileIOException raised (absolutely normal).

Does anyone have an idea of how to check if a file can be written to?

Thanks
Jeff
 
Typo: It's not FileIOException but System.IO.IoException :p
 
Try
' open the file for append
Catch ex As System.IO. ...
' code block_1
Catch es As something else
' code block_1
Finally
' code to be executed anyway
End Try
 
Is it really good programming practice?

If I do what you suggest, I would have something like this:

Try
.
.
.

' Opens writer
swFile = New System.IO.StreamWriter(FilePath, False, System.Text.Encoding.Unicode)

Return True

Catch exIO AS System.IO.IOException
' Writer cannot open file, writes in C:\ instead
' Changes path of file
FilePath = "C:" & Mid(FilePath, InStrRev(FilePath, "\"), Len(FilePath))

' Writes new key file including activation code
swFile = New System.IO.StreamWriter(FilePath, False, System.Text.Encoding.Unicode)

Return True

Catch ex AS Exception
' Do something
Finally
swFile.WriteLine("SOME DATA1")
swFile.WriteLine("SOME DATA2")
swFile.WriteLine("SOME DATA3")

swFile.Close()

End Try
 
I think the return true is useless.
With try/catch block you can check if the file is in use. But if you know an other way, it is ok too.
 
My code is in a function and needs the Return True to perform additional tasks :)

Well I'll stick with your suggestion. ;) If someone else has another method let me know.

Thanks TipGiver
 
Thnx for the *.
Of cource, if it is a function!
 
You can do it without catching an exception:

Code:
Private Function IsFileReadonly(ByVal filename As String) As Boolean
  Dim fa As FileAttributes = File.GetAttributes(filename)
  Return fa.ToString.IndexOf("ReadOnly") <> -1
End Function

Just call this function with the complete filename and path to check wether it is readonly or not.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Ruffnekk,

I can't check only for the FileAttribute since the file may be read-only or not. If it is read-only, fine noone can modify it and can check it with your function. Now if it is not read-only, the user may or may not have write access on the file. Either because of OS security settings, or example if the file is on a write-protected diskette.
 
Hmmm.. I was thinking it would return readonly even when the attribute is not set but the media is readonly... maybe I'm wrong.

Regards, Ruffnekk
---
Is it my imagination or do buffalo wings taste just like chicken?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top