fixed #3584: Handle invalid personal folder path in application settings

This commit is contained in:
Jaex 2018-08-25 15:51:37 +03:00
parent 93e077a691
commit 7f84c812d4
3 changed files with 46 additions and 20 deletions

View file

@ -884,6 +884,21 @@ public static void CreateDirectoryFromFilePath(string path)
} }
} }
public static bool IsValidFilePath(string path)
{
FileInfo fi = null;
try
{
fi = new FileInfo(path);
}
catch (ArgumentException) { }
catch (PathTooLongException) { }
catch (NotSupportedException) { }
return fi != null;
}
public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true) public static void CopyFile(string filePath, string destinationFolder, bool overwrite = true)
{ {
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder)) if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder))

View file

@ -65,13 +65,15 @@ private void SettingsForm_FormClosed(object sender, FormClosedEventArgs e)
{ {
string currentPersonalPath = txtPersonalFolderPath.Text; string currentPersonalPath = txtPersonalFolderPath.Text;
if (!currentPersonalPath.Equals(lastPersonalPath, StringComparison.InvariantCultureIgnoreCase)) if (!currentPersonalPath.Equals(lastPersonalPath, StringComparison.InvariantCultureIgnoreCase) &&
(string.IsNullOrEmpty(currentPersonalPath) || Helpers.IsValidFilePath(currentPersonalPath)))
{
if (Program.WritePersonalPathConfig(currentPersonalPath))
{ {
Program.WritePersonalPathConfig(currentPersonalPath);
MessageBox.Show("You must reopen ShareX for personal folder changes to take effect.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information); MessageBox.Show("You must reopen ShareX for personal folder changes to take effect.", "ShareX", MessageBoxButtons.OK, MessageBoxIcon.Information);
} }
} }
}
private void tttvMain_TabChanged(TabPage tabPage) private void tttvMain_TabChanged(TabPage tabPage)
{ {
@ -300,6 +302,8 @@ private void UpdatePersonalFolderPathPreview()
{ {
string personalPath = txtPersonalFolderPath.Text; string personalPath = txtPersonalFolderPath.Text;
try
{
if (string.IsNullOrEmpty(personalPath)) if (string.IsNullOrEmpty(personalPath))
{ {
if (Program.PortableApps) if (Program.PortableApps)
@ -317,12 +321,16 @@ private void UpdatePersonalFolderPathPreview()
} }
else else
{ {
personalPath = Helpers.ExpandFolderVariables(personalPath);
personalPath = Helpers.GetAbsolutePath(personalPath); personalPath = Helpers.GetAbsolutePath(personalPath);
} }
lblPreviewPersonalFolderPath.Text = personalPath; lblPreviewPersonalFolderPath.Text = personalPath;
} }
catch (Exception e)
{
lblPreviewPersonalFolderPath.Text = "Error: " + e.Message;
}
}
#region General #region General

View file

@ -447,7 +447,7 @@ public static string ReadPersonalPathConfig()
return ""; return "";
} }
public static void WritePersonalPathConfig(string path) public static bool WritePersonalPathConfig(string path)
{ {
if (path == null) if (path == null)
{ {
@ -470,6 +470,7 @@ public static void WritePersonalPathConfig(string path)
{ {
Helpers.CreateDirectoryFromFilePath(PersonalPathConfigFilePath); Helpers.CreateDirectoryFromFilePath(PersonalPathConfigFilePath);
File.WriteAllText(PersonalPathConfigFilePath, path, Encoding.UTF8); File.WriteAllText(PersonalPathConfigFilePath, path, Encoding.UTF8);
return true;
} }
catch (UnauthorizedAccessException) catch (UnauthorizedAccessException)
{ {
@ -478,6 +479,8 @@ public static void WritePersonalPathConfig(string path)
} }
} }
} }
return false;
} }
private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e) private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)