Reputation: 706
I am trying to use streamwriter to create a text file in the my documents folder, however it thinks i am using a relative path when i am actually using a full path.
I am trying to create the file using this path: "%HOMEPATH%/My Documents/", but it treats this as a relative path.
Any help would be appreciated, thanks.
Upvotes: 3
Views: 2446
Reputation: 105
Check the environment variables from the command prompt. I see the following on my machine -
HOMEDRIVE=C:
HOMEPATH=\Users\foo
So try %HOMEDRIVE%%HOMEPATH% instead to fix your problem
Upvotes: 0
Reputation: 9237
I would resolve the environment variable using some API and then input the path that includes the absolute path ("C:.....") to the StreamWriter
You could use Environment.SpecialFolder Enumeration as suggested by David Staratton.
Upvotes: 0
Reputation: 1500405
You should use Environment.GetFolderPath
- which in this case would avoid you hard-coding My Documents
at all:
string docs = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
If you wanted to get a directory name relative to that, you should use Path.Combine
:
string fooDocsDirectory = Path.Combine(docs, "foo");
Upvotes: 10
Reputation: 52788
The correct way to get a users' Documents folder in .Net is to use Environment.GetFolderPath()
and pass in Environment.SpecialFolder.MyDocuments
.
Upvotes: 4
Reputation: 73564
Use System.Environment.SpecialFolder.MyDocuments to get to the My Documents path, rather than attempting to use system variables.
Upvotes: 3