Pass the path if there are environmental variables exist in the path then it will be collapsed into a valid path and return the valid path.
Constants :-
public const string PATH_SEPARATOR = @"\";
public const string ENVIROMENT_VARIABLE_FORMAT = "%{0}%";
public static string CollapseEnviromentVariables(string pathString)
{
string result = pathString;
if (string.IsNullOrEmpty(pathString) == false)
{
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
Dictionary matchingEnv = new Dictionary();
bool isReplaceEnv = false;
foreach (DictionaryEntry environmentVariable in environmentVariables)
{
if ((pathString.Length >= ((string)environmentVariable.Value).Length) && (pathString.Contains((string)environmentVariable.Value)))
{
isReplaceEnv = false;
foreach (string matchingEnvEntry in matchingEnv.Keys)
{
if ((((string)matchingEnv[matchingEnvEntry]).Length <= ((string)environmentVariable.Value).Length) &&
((string)environmentVariable.Value).Contains((string)matchingEnv[matchingEnvEntry]))
{
matchingEnv.Remove(matchingEnvEntry);
matchingEnv.Add((string)environmentVariable.Key, (string)environmentVariable.Value);
isReplaceEnv = true;
break;
}
}
if (isReplaceEnv == false)
{
matchingEnv.Add((string)environmentVariable.Key, (string)environmentVariable.Value);
}
}
}
int matchingEnvIndex = 0;
int matchingEnvLength = 0;
foreach (string matchingEnvEntry in matchingEnv.Keys)
{
matchingEnvIndex = result.IndexOf(matchingEnv[matchingEnvEntry]);
matchingEnvLength = matchingEnv[matchingEnvEntry].Length;
//Replace environment variable only if environment variable value is enclosed by path seperators
//and matched value is found after replacing other variables.
if ((matchingEnvIndex > -1) &&
((matchingEnvIndex == 0) || (result[matchingEnvIndex - 1].ToString() == Constants.PATH_SEPARATOR)) &&
((matchingEnvIndex + matchingEnvLength == result.Length) || (result[matchingEnvIndex + matchingEnvLength].ToString() == Constants.PATH_SEPARATOR)))
{
result = result.Replace(matchingEnv[matchingEnvEntry], string.Format(Constants.ENVIROMENT_VARIABLE_FORMAT, matchingEnvEntry));
}
}
}
return result;
}
Constants :-
public const string PATH_SEPARATOR = @"\";
public const string ENVIROMENT_VARIABLE_FORMAT = "%{0}%";
public static string CollapseEnviromentVariables(string pathString)
{
string result = pathString;
if (string.IsNullOrEmpty(pathString) == false)
{
IDictionary environmentVariables = Environment.GetEnvironmentVariables();
Dictionary
bool isReplaceEnv = false;
foreach (DictionaryEntry environmentVariable in environmentVariables)
{
if ((pathString.Length >= ((string)environmentVariable.Value).Length) && (pathString.Contains((string)environmentVariable.Value)))
{
isReplaceEnv = false;
foreach (string matchingEnvEntry in matchingEnv.Keys)
{
if ((((string)matchingEnv[matchingEnvEntry]).Length <= ((string)environmentVariable.Value).Length) &&
((string)environmentVariable.Value).Contains((string)matchingEnv[matchingEnvEntry]))
{
matchingEnv.Remove(matchingEnvEntry);
matchingEnv.Add((string)environmentVariable.Key, (string)environmentVariable.Value);
isReplaceEnv = true;
break;
}
}
if (isReplaceEnv == false)
{
matchingEnv.Add((string)environmentVariable.Key, (string)environmentVariable.Value);
}
}
}
int matchingEnvIndex = 0;
int matchingEnvLength = 0;
foreach (string matchingEnvEntry in matchingEnv.Keys)
{
matchingEnvIndex = result.IndexOf(matchingEnv[matchingEnvEntry]);
matchingEnvLength = matchingEnv[matchingEnvEntry].Length;
//Replace environment variable only if environment variable value is enclosed by path seperators
//and matched value is found after replacing other variables.
if ((matchingEnvIndex > -1) &&
((matchingEnvIndex == 0) || (result[matchingEnvIndex - 1].ToString() == Constants.PATH_SEPARATOR)) &&
((matchingEnvIndex + matchingEnvLength == result.Length) || (result[matchingEnvIndex + matchingEnvLength].ToString() == Constants.PATH_SEPARATOR)))
{
result = result.Replace(matchingEnv[matchingEnvEntry], string.Format(Constants.ENVIROMENT_VARIABLE_FORMAT, matchingEnvEntry));
}
}
}
return result;
}