I have a small VB app that needs to create a log file, and Ideally I'd like that on the users desktop as it's easy to see.
However, when I try to create the file (a *.txt file), I get the following error -
A first chance exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Now, it seems pretty obvious that it's a permissions issue, but I don't know how to work around it, so I wonder if someone can help me out? Thanks.
Full code for creating the file (taken from MSDN, and modifided very slightly) -
Imports System
Imports System.IO
Imports System.Text
Module write_text
Public Class Log_File
'*'
' Createa a log file on the users desktop and adds the 'output' text to it
'
' @param required string output The text to output to the log
'*'
Public Shared Sub write_to_file(ByVal output As String)
Dim path As String ' The path to where the file to create and write should be saved
Dim file_name As String ' The name of the log file that is to be created
Debug.Print(vbCrLf & " write_to_file(){" & vbCrLf)
' Set the error handler
On Error GoTo ExitError
path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
file_name = "MusicRenaming - " & Date.Today & ".txt"
Debug.Print(" Log file: " & path & "\" & file_name)
' Check if the file exists
If File.Exists(path) = False Then
' Create a file to write too.
Dim sw As StreamWriter = File.CreateText(path)
sw.WriteLine("Hello")
sw.WriteLine("And")
sw.WriteLine("Welcome")
sw.Flush()
sw.Close()
Debug.Print(" File had to be created")
End If
' Open the file to write too
Dim sr As StreamReader = File.OpenText(path)
Do While sr.Peek() >= 0
Console.WriteLine(sr.ReadLine())
Debug.Print(" Output to file complete")
Loop
' Close the file
sr.Close()
Debug.Print(vbCrLf & " }")
ExitError:
If Err.Number <> 0 Then functions.error_handler()
End Sub
End Class
End Module
Please none - I realise that the function isn't particularly operational right now, I'm just trying to get the file created first, then I'll work on that.
Thanks to an answer by @RubensFarias posted in this thread I was able to fix this problem.
I have also incorporated the try...catch
method as suggested by @minitech in the comments to my origianl question, so hopefully he is satisfied that I addressed his concerns :)
Imports System.IO
Module write_text
Public Class Log_File
'*'
' Createa a log file on the users desktop and adds the 'output' text to it
'
' @param required string output The text to output to the log
'*'
Public Shared Sub write_to_file(ByVal output As String)
Dim file_name As String ' The name of the log file that is to be created
Dim file_path As String ' The full path to where the file to create and write should be saved
Dim file_exists As Boolean ' Whether or not the file already exists
Try
file_name = "MusicRenaming - " & DateTime.Today.ToString("dd-mm-yyyy") & ".txt"
file_path = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) & "\" & file_name
file_exists = File.Exists(file_path)
Using sw As New StreamWriter(File.Open(file_path, FileMode.OpenOrCreate))
sw.WriteLine(output)
End Using
Catch oError As Exception
functions.error_handler(oError)
End Try
End Sub
End Class
End Module