Skip to content

Commit ac1558b

Browse files
Fix fatal exception at startup
The local application data directory was not properly checked before being accessed. This fixed #34.
1 parent 1134996 commit ac1558b

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

Gmail notifier/Main.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ private enum Privacy:int {
4848
// number of automatic reconnection
4949
private int reconnect = 0;
5050

51+
// local application data folder name
52+
private string appdata = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Gmail Notifier";
53+
5154
// number of maximum automatic reconnection
5255
private const int MAX_AUTO_RECONNECT = 3;
5356

@@ -73,7 +76,7 @@ private void Main_Load(object sender, EventArgs e) {
7376
Visible = false;
7477

7578
// displays a systray notification on first load
76-
if (Settings.Default.FirstLoad && !Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Gmail Notifier").Any()) {
79+
if (Settings.Default.FirstLoad && !Directory.Exists(appdata)) {
7780
notifyIcon.ShowBalloonTip(7000, translation.welcome, translation.firstLoad, ToolTipIcon.Info);
7881

7982
// switchs the first load state
@@ -257,7 +260,7 @@ private async void AsyncAuthentication() {
257260
} catch(Exception) {
258261

259262
// exits the application if the google api token file doesn't exists
260-
if (!Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Gmail Notifier").Any()) {
263+
if (!Directory.Exists(appdata) || !Directory.EnumerateFiles(appdata).Any()) {
261264
MessageBox.Show(translation.authenticationWithGmailRefused, translation.authenticationFailed, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
262265
Application.Exit();
263266
}
@@ -283,7 +286,7 @@ private async Task<UserCredential> AsyncAuthorizationBroker() {
283286
new string[] { GmailService.Scope.GmailModify },
284287
"user",
285288
CancellationToken.None,
286-
new FileDataStore(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Gmail Notifier", true)
289+
new FileDataStore(appdata, true)
287290
);
288291
}
289292
}
@@ -726,7 +729,9 @@ private void buttonGmailDisconnect_Click(object sender, EventArgs e) {
726729
}
727730

728731
// deletes the local application data folder and the client token file
729-
Directory.Delete(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "/Gmail Notifier", true);
732+
if (Directory.Exists(appdata)) {
733+
Directory.Delete(appdata, true);
734+
}
730735

731736
// restarts the application
732737
this.restart();

0 commit comments

Comments
 (0)