📄 filepathhelper.cs
字号:
namespace Imps.Utils
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
public static class FilePathHelper
{
public const string ConfigPatternAppBaseDir = "AppBase";
public const string ConfigPatternAppData = "AppData";
public const string ConfigPatternComAppData = "ComAppData";
public static string GetPathWithEnvVar(string path)
{
MatchCollection matchs = new Regex(@"%\w*%").Matches(path);
Dictionary<string, string> dictionary = new Dictionary<string, string>();
for (int i = 0; i < matchs.Count; i++)
{
string text = matchs[i].Value;
string variable = text.Substring(1, text.Length - 2);
string appBaseDir = "";
switch (variable)
{
case "AppBase":
appBaseDir = AppBaseDir;
break;
case "ComAppData":
appBaseDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
break;
case "AppData":
appBaseDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
break;
default:
appBaseDir = Environment.GetEnvironmentVariable(variable);
if (appBaseDir == null)
{
appBaseDir = "";
}
break;
}
if ((appBaseDir.Length > 0) && (appBaseDir[appBaseDir.Length - 1] == '\\'))
{
appBaseDir = appBaseDir.Substring(0, appBaseDir.Length - 1);
}
dictionary.Add(text, appBaseDir);
}
Dictionary<string, string>.KeyCollection.Enumerator enumerator = dictionary.get_Keys().GetEnumerator();
try
{
while (enumerator.MoveNext())
{
string oldValue = enumerator.get_Current();
path = path.Replace(oldValue, dictionary.get_Item(oldValue).ToString());
}
}
finally
{
enumerator.Dispose();
}
return path.Replace(@"\\", @"\");
}
public static void MakeSureFilePathExists(string filePath)
{
if (((filePath != null) && (filePath.Length > 0)) && (filePath.LastIndexOf('\\')++ > 0))
{
int length;
if (length < filePath.Length)
{
filePath = filePath.Substring(0, length);
}
if (!Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
}
}
public static string AppBaseDir
{
get
{
return Path.GetDirectoryName(Application.ExecutablePath);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -