File I/O works in debug mode but not in executable

shingabiss

New member
Joined
Aug 16, 2013
Messages
2
Location
California
Programming Experience
10+
My code works fine to write to a file in the debug mode but when I run the executable I get a "access denied" error. The code is below:

Dim DateOfTest AsDate
Dim TesterName AsString
Dim SerialNumber AsString
TesterName = TextBoxTester.Text
DateOfTest = Now
SerialNumber = TextBoxBoardSN.Text
'File writing for test info
Dim path AsString = "C:\" & SerialNumber & ".txt"
'set "append argument" variable for streamwriter to append or write
If File.Exists(path) Then
StreamWriterTrueFalse = True'flle exists so append
Else
StreamWriterTrueFalse = False'file doesn't exist so create
EndIf
Dim writer As StreamWriter = New StreamWriter(path, StreamWriterTrueFalse)
writer.WriteLine(
"CPX Loopback Test " & vbCrLf)
writer.WriteLine(
"Serial Number " & vbTab & "Tester Name" & vbTab & "Date")
writer.WriteLine(SerialNumber & vbTab & vbTab & TesterName & vbTab & vbTab & DateOfTest & vbCrLf)
 
No it didn't. What I ended up doing (still doesn't answer the question though) is to create a dir one level above the root and that seems to work, don't know why.
 
Writing to the root of the system drive is dodgy any time. Windows doesn't like it and for good reason. Your application should either be prompting the user to select a location to save the file or else using one of the standard locations, the paths of which can be access via Environment.GetFolderPath or My.Computer.FileSystem.SpecialDirectories. They also account for the fact that those paths may be different on different systems. For instance, even if your code did work, what about the small percentage of users who haven't installed on C:?
 
By the way, in future, please don't post your code as HTML because it's harder to read. Please post it as plain text within
 tags, which will format the code you and make it easy for us to read.  Most importantly, it will retain indenting.
 
Back
Top