Expand environment variable for My Documents

无人久伴 提交于 2019-12-30 06:00:13

问题


I know I can read environment variables like this:

Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

However, it would be really helpful to me if I could do something like this:

Environment.ExpandEnvironmentVariables(@"%MyDocuments%\Foo");

Is there an environement variable that equals SpecialFolder.MyDocuments?

I also tried to do something like this, but this doesn't lead to the expected result:

Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\My Documents\Foo");

This way I ended up with something like @"C:\Users\<MyUser>\My Documents\Foo" but I what I need is @"\\someservername\users$\<MyUser>\My Documents\Foo".

EDIT: My Goal is NOT to hardcode either environment variable nor the part after that.

Any other suggestions?


回答1:


No there is no environment variable for the MyDocuments special folder (the same is true for most members of the SpecialFolder enumeration).

Check out this snippet, it might be exactly what you are searching for.

It allows you to do something like that:

string fullPath = SpecialFolder.ExpandVariables(@"%MyDocuments%\Foo");

Note: SpecialFolder.ExpandVariables is a static method of a helper class introduced in the above snippet.




回答2:


Is there an environment variable that equals SpecialFolder.MyDocuments?

Short answer: No.

Long answer:
Still no. You can type "set" into a Command Prompt to see all you current environment variables. I couldn't find any for my documents folder on my profile (tried on WinXP and Win7).

Also, expanding "%USERPROFILE%\My Documents" would be incorrect since the user's documents folder could be anywhere else (e.g., on my home PC I always change mine to D:\Documents).

If you really need to use environment variables, one solution might be to set the variable yourself:

// this environment variable is created for the current process only
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Environment.SetEnvironmentVariable("MYDOCUMENTS", documents);

Another solution might be to use a "fake" environment variable in the path and expand it yourself, something like:

string path = "%MYDOCUMENTS%\\Foo"; // read from config

// expand real env. vars
string expandedPath1 = Environment.ExpandEnvironmentVariables(path);

// expand our "fake" env. var
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string expandedPath2 = path.Replace("%MYDOCUMENTS%", documents);



回答3:


I'm not sure if there is a good way to do this but instead of trying to do environment expansion to get the path why not use the Path.Combine API instead?

Path.Combine(
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
  "Foo");



回答4:


What exactly are you trying to do? Is there any reason why you can't just use Path.Combine?

string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string foo = Path.Combine(docs, "Foo");



回答5:


You can expand environment variables using then Environment.GetEnvironmentVariable method. Given your comment, I would suggest breaking your path up into 2 separate config settings to make expanding it easier:

string variablePath = "%appdata%".Trim('%'); //read from some config setting
string appdataPath = Environment.GetEnvironmentVariable(variablePath);
string subdir = "foo";  //some other config setting
string myDir = Path.Combine(appdataPath, subdir);



回答6:


No it does not exist. The easiest way to check is to run "set" from command line and see yourself.

Start-> run -> cmd
set
set |findstr /i documents


来源:https://stackoverflow.com/questions/4726818/expand-environment-variable-for-my-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!