@@ -12,57 +12,62 @@ Imports System.IO
1212Public Module WinNUT_Globals
1313
1414# Region "Constants/Shareds"
15- Private DEFAULT_DATA_PATH As String
16- # End Region
1715
18- Public LongProgramName As String
19- Public ProgramName As String
20- Public ProgramVersion As String
21- Public ShortProgramVersion As String
22- Public GitHubURL As String
23- Public Copyright As String
24- Public IsConnected As Boolean
16+ # If DEBUG Then
17+ Public ReadOnly IsDebugBuild = True
18+ ' If debugging, keep any generated data next to the debug executable.
19+ Private ReadOnly DESIRED_DATA_PATH As String = Path.Combine(Environment.CurrentDirectory)
20+ # Else
21+ Public ReadOnly IsDebugBuild = False
22+ Private ReadOnly DESIRED_DATA_PATH As String = Environment.GetFolderPath(
23+ Environment.SpecialFolder.ApplicationData))
24+ # End If
25+
26+ Private ReadOnly FALLBACK_DATA_PATH = Path.GetTempPath()
27+ Private ReadOnly DATA_DIRECTORY_NAME = "WinNut-Client"
28+
29+ Public ReadOnly ProgramName = My.Application.Info.ProductName
30+ Public ReadOnly LongProgramName = My.Application.Info.Description
31+ Public ReadOnly ProgramVersion = Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString
32+ Public ReadOnly ShortProgramVersion = ProgramVersion.Substring( 0 , ProgramVersion.IndexOf( "." , ProgramVersion.IndexOf( "." ) + 1 ))
33+ Public ReadOnly GitHubURL = My.Application.Info.Trademark
34+ Public ReadOnly Copyright = My.Application.Info.Copyright
35+
36+ Public IsConnected = False
2537 Public ApplicationData As String
26- ' Public LogFile As String
27- ' Handle application messages and debug events.
28- ' Public WithEvents LogFile As Logger ' As New Logger(False, 0)
29- ' Logging
30- Public WithEvents LogFile As Logger
38+ Public WithEvents LogFile As Logger = New Logger(LogLvl.LOG_DEBUG)
3139 Public AppIcon As Dictionary( Of Integer , Drawing.Icon)
3240 Public StrLog As New List( Of String )
33- ' Public LogFilePath As String
3441
35- Public Sub Init_Globals()
36- # If DEBUG Then
37- ' If debugging, keep any generated data next to the debug executable.
38- DEFAULT_DATA_PATH = Path.Combine(Environment.CurrentDirectory, "Data" )
39- # Else
40- DEFAULT_DATA_PATH = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\WinNUT-Client" )
41- # End If
42+ # End Region
4243
43- LongProgramName = My.Application.Info.Description
44- ProgramName = My.Application.Info.ProductName
45- ProgramVersion = Reflection.Assembly.GetEntryAssembly().GetName().Version.ToString
46- ShortProgramVersion = ProgramVersion.Substring( 0 , ProgramVersion.IndexOf( "." , ProgramVersion.IndexOf( "." ) + 1 ))
47- GitHubURL = My.Application.Info.Trademark
48- Copyright = My.Application.Info.Copyright
49- IsConnected = False
50- LogFile = New Logger(LogLvl.LOG_DEBUG)
5144
52- SetupAppDirectory()
53- End Sub
5445
55- Sub SetupAppDirectory()
56- If Not Directory.Exists(DEFAULT_DATA_PATH) Then
57- Try
58- Directory.CreateDirectory(DEFAULT_DATA_PATH)
59- ApplicationData = DEFAULT_DATA_PATH
60- Catch ex As Exception
61- LogFile.LogTracing(ex.ToString & " encountered trying to create app data directory. Falling back to temp." ,
62- LogLvl.LOG_ERROR, Nothing )
63- ApplicationData = Path.GetTempPath() & "\WinNUT_Data\"
64- Directory.CreateDirectory(ApplicationData)
65- End Try
66- End If
46+ Public Sub Init_Globals()
47+ ApplicationData = GetAppDirectory(DESIRED_DATA_PATH)
6748 End Sub
49+
50+ ''' <summary>
51+ ''' Do everything possible to find a safe place to write to, with the <see cref="ProgramName"/> appended to it. If
52+ ''' the requested option is unavailable, we fall back to the temporary directory for the current user.
53+ ''' </summary>
54+ ''' <param name="requestedDir">The requested directory, with <see cref="ProgramName"/> appended to it.</param>
55+ ''' <returns>The best possible option available as a writable data directory.</returns>
56+ Private Function GetAppDirectory(requestedDir As String ) As String
57+ requestedDir = Path.Combine(requestedDir, DATA_DIRECTORY_NAME)
58+
59+ Try
60+ Directory.CreateDirectory(requestedDir)
61+ LogFile.LogTracing( "Successfully created or opened requested data directory for WinNUT." &
62+ vbNewLine & "requestedDir: " & requestedDir, LogLvl.LOG_DEBUG, Nothing )
63+ Return requestedDir
64+
65+ Catch ex As Exception
66+ LogFile.LogTracing(ex.ToString & " encountered trying to create app data directory. Falling back to temp." ,
67+ LogLvl.LOG_ERROR, Nothing )
68+
69+ Directory.CreateDirectory(FALLBACK_DATA_PATH)
70+ Return FALLBACK_DATA_PATH
71+ End Try
72+ End Function
6873End Module
0 commit comments