将完整路径变成带有环境变量的路径

我想使用 C# 将完整路径转换为环境变量路径

这可能吗?

IE

C:\Users\Username\Documents\Text.txt -> %USERPROFILE%\Documents\Text.txt
C:\Windows\System32\cmd.exe -> %WINDIR%\System32\cmd.exe
C:\Program Files\Program\Program.exe -> %PROGRAMFILES%\Program\Program.exe


DIEA
浏览 79回答 1
1回答

素胚勾勒不出你

可以通过检查所有环境变量并检查字符串中包含哪个变量的值,然后将字符串的该部分替换为%.第一次天真的尝试:string Tokenify(string path){&nbsp; &nbsp; foreach (DictionaryEntry e in Environment.GetEnvironmentVariables())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int index = path.IndexOf(e.Value.ToString());&nbsp; &nbsp; &nbsp; &nbsp; if (index > -1)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //we need to make sure we're not already inside a tokenized part.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int numDelimiters = path.Take(index).Count(c => c == '%');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (numDelimiters % 2 == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; path = path.Replace(e.Value.ToString(), $"%{e.Key.ToString()}%");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return path;}该代码当前错误地假设环境变量的值在路径中仅出现一次。这需要纠正,但我们暂时把它放在一边。另请注意,并非所有环境变量都代表目录。例如,如果我在字符串 上运行此方法"6",我会得到"%PROCESSOR_LEVEL%"。Directory.Exists()这可以通过在使用环境变量值之前检查它来解决。这也可能使得检查我们是否已经处于字符串的标记化部分中的需要变得无效。您可能还想按长度对环境变量进行排序,以便始终使用最具体的变量。否则你可能会得到:%HOMEDRIVE%%HOMEPATH%\AppData\Local\Folder代替:%LOCALAPPDATA%\Folder更新了更喜欢最长变量的代码:string Tokenify(string path){&nbsp; &nbsp; //first find all the environment variables that represent paths.&nbsp; &nbsp; var validEnvVars = new List<KeyValuePair<string, string>>();&nbsp; &nbsp; foreach (DictionaryEntry e in Environment.GetEnvironmentVariables())&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; string envPath = e.Value.ToString();&nbsp; &nbsp; &nbsp; &nbsp; if (System.IO.Directory.Exists(envPath))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //this would be the place to add any other filters.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validEnvVars.Add(new KeyValuePair<string, string>(e.Key.ToString(), envPath));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; //sort them by length so we always get the most specific one.&nbsp; &nbsp; //if you are dealing with a large number of strings then orderedVars can be generated just once and cached.&nbsp; &nbsp; var orderedVars = validEnvVars.OrderByDescending(kv => kv.Value.Length);&nbsp; &nbsp; foreach (var kv in orderedVars)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; //using regex just for case insensitivity. Otherwise just use string.Replace.&nbsp; &nbsp; &nbsp; &nbsp; path = Regex.Replace(path, Regex.Escape(kv.Value), $"%{kv.Key}%", RegexOptions.IgnoreCase);&nbsp; &nbsp; }&nbsp; &nbsp; return path;}您可能仍然希望添加检查以避免对字符串的部分进行双重标记,但这在此版本中不太可能成为问题。此外,您可能想过滤掉一些变量,例如驱动器根,例如 ( %HOMEDRIVE%) 或通过任何其他条件。
打开App,查看更多内容
随时随地看视频慕课网APP