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)
{
if (!string.IsNullOrEmpty(filePath) && !string.IsNullOrEmpty(destinationFolder))

View file

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

View file

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