Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# getting the path of %AppData%

Tags:

c#

.net

path

C# 2008 SP1

I am using the code below:

dt.ReadXml("%AppData%\\DateLinks.xml"); 

However, I am getting an exception that points to the location of where my application is running from:

Could not find a part of the path 'D:\Projects\SubVersionProjects\CatDialer\bin\Debug\%AppData%\DateLinks.xml'.

I thought the %AppData% should find the relative path. When I go Start|Run|%AppData% windows explorer takes me to that directory.

I can not put the full path in, as the user is different on each client machine.

like image 785
ant2009 Avatar asked May 15 '09 08:05

ant2009


People also ask

Bahasa C digunakan untuk apa?

Meskipun C dibuat untuk memprogram sistem dan jaringan komputer namun bahasa ini juga sering digunakan dalam mengembangkan software aplikasi. C juga banyak dipakai oleh berbagai jenis platform sistem operasi dan arsitektur komputer, bahkan terdapat beberepa compiler yang sangat populer telah tersedia.

Apa yang dimaksud dengan huruf C?

C adalah huruf ketiga dalam alfabet Latin. Dalam bahasa Indonesia, huruf ini disebut ce (dibaca [tʃe]).


2 Answers

To get the AppData directory, it's best to use the GetFolderPath method:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) 

(must add using System if not present).

%AppData% is an environment variable, and they are not automatically expanded anywhere in .NET, although you can explicitly use the Environment.ExpandEnvironmentVariable method to do so. I would still strongly suggest that you use GetFolderPath however, because as Johannes Rössel points out in the comment, %AppData% may not be set in certain circumstances.

Finally, to create the path as shown in your example:

var fileName = Path.Combine(Environment.GetFolderPath(     Environment.SpecialFolder.ApplicationData), "DateLinks.xml"); 
like image 125
Noldorin Avatar answered Oct 07 '22 22:10

Noldorin


The BEST way to use the AppData directory, IS to use Environment.ExpandEnvironmentVariables method.

Reasons:

  • it replaces parts of your string with valid directories or whatever
  • it is case-insensitive
  • it is easy and uncomplicated
  • it is a standard
  • good for dealing with user input

Examples:

string path; path = @"%AppData%\stuff"; path = @"%aPpdAtA%\HelloWorld"; path = @"%progRAMfiLES%\Adobe;%appdata%\FileZilla"; // collection of paths  path = Environment.ExpandEnvironmentVariables(path); Console.WriteLine(path); 

More info:

%ALLUSERSPROFILE%   C:\ProgramData %APPDATA%   C:\Users\Username\AppData\Roaming %COMMONPROGRAMFILES%    C:\Program Files\Common Files %COMMONPROGRAMFILES(x86)%   C:\Program Files (x86)\Common Files %COMSPEC%   C:\Windows\System32\cmd.exe %HOMEDRIVE% C: %HOMEPATH%  C:\Users\Username %LOCALAPPDATA%  C:\Users\Username\AppData\Local %PROGRAMDATA%   C:\ProgramData %PROGRAMFILES%  C:\Program Files %PROGRAMFILES(X86)% C:\Program Files (x86) (only in 64-bit version) %PUBLIC%    C:\Users\Public %SystemDrive%   C: %SystemRoot%    C:\Windows %TEMP% and %TMP%    C:\Users\Username\AppData\Local\Temp %USERPROFILE%   C:\Users\Username %WINDIR%    C:\Windows 
like image 35
Bitterblue Avatar answered Oct 07 '22 22:10

Bitterblue