Supports all SpecialFolder enums

This commit is contained in:
Michael Delpach 2016-01-16 20:15:27 +08:00
parent 52c77a034d
commit 8a4e57ffc6
5 changed files with 14 additions and 9 deletions

View file

@ -607,7 +607,7 @@ public static bool BrowseFile(string title, TextBox tb, string initialDirectory
return false;
}
public static bool BrowseFolder(string title, TextBox tb, string initialDirectory = "")
public static bool BrowseFolder(string title, TextBox tb, string initialDirectory = "", bool detectSpecialFolders = true)
{
using (FolderSelectDialog fsd = new FolderSelectDialog())
{
@ -626,7 +626,7 @@ public static bool BrowseFolder(string title, TextBox tb, string initialDirector
if (fsd.ShowDialog())
{
tb.Text = GetVariableFolderPath(fsd.FileName);
tb.Text = detectSpecialFolders ? GetVariableFolderPath(fsd.FileName) : fsd.FileName;
return true;
}
}
@ -636,15 +636,20 @@ public static bool BrowseFolder(string title, TextBox tb, string initialDirector
public static string GetVariableFolderPath(string folderPath)
{
folderPath = folderPath.Replace(Environment.GetFolderPath(Environment.SpecialFolder.MyPictures), "%MyPictures%", StringComparison.InvariantCultureIgnoreCase);
folderPath = folderPath.Replace(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "%UserProfile%", StringComparison.InvariantCultureIgnoreCase);
if (Directory.Exists(folderPath))
{
Helpers.GetEnums<Environment.SpecialFolder>().ForEach(x => folderPath = folderPath.Replace(Environment.GetFolderPath(x), string.Format("%{0}%", x), StringComparison.InvariantCultureIgnoreCase));
}
return folderPath;
}
public static string ExpandFolderVariables(string folderPath)
{
folderPath = folderPath.Replace("%MyPictures%", Environment.GetFolderPath(Environment.SpecialFolder.MyPictures));
if (Directory.Exists(folderPath))
{
Helpers.GetEnums<Environment.SpecialFolder>().ForEach(x => folderPath = folderPath.Replace(string.Format("%{0}%", x), Environment.GetFolderPath(x), StringComparison.InvariantCultureIgnoreCase));
}
return Environment.ExpandEnvironmentVariables(folderPath);
}

View file

@ -43,7 +43,7 @@ public override object EditValue(ITypeDescriptorContext context, IServiceProvide
dlg.Title = Resources.DirectoryNameEditor_EditValue_Browse_for_a_folder___;
if (dlg.ShowDialog())
{
value = dlg.FileName;
value = Helpers.GetVariableFolderPath(dlg.FileName);
}
}
return value;

View file

@ -253,7 +253,7 @@ private void UpdatePersonalFolderPathPreview()
}
else
{
personalPath = Environment.ExpandEnvironmentVariables(personalPath);
personalPath = Helpers.ExpandFolderVariables(personalPath);
personalPath = Helpers.GetAbsolutePath(personalPath);
}

View file

@ -476,7 +476,7 @@ private static void CheckPersonalPathConfig()
if (!string.IsNullOrEmpty(customPersonalPath))
{
customPersonalPath = Environment.ExpandEnvironmentVariables(customPersonalPath);
customPersonalPath = Helpers.ExpandFolderVariables(customPersonalPath);
CustomPersonalPath = Helpers.GetAbsolutePath(customPersonalPath);
}
else if (IsPortableApps)

View file

@ -235,7 +235,7 @@ public string CaptureFolder
{
if (!string.IsNullOrEmpty(AdvancedSettings.CapturePath))
{
return AdvancedSettings.CapturePath;
return Helpers.ExpandFolderVariables(AdvancedSettings.CapturePath);
}
return Program.ScreenshotsFolder;