C# getting the path of %AppData%

后端 未结 10 2113
无人共我
无人共我 2020-11-22 13:50

C# 2008 SP1

I am using the code below:

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

However, I am getting an exception that points t

相关标签:
10条回答
  • 2020-11-22 14:20

    The BEST way to use the AppData directory, IS to use Environment.ExpandEnvironmentVariable 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
    
    0 讨论(0)
  • 2020-11-22 14:25

    Just wanted to share another way of accessing 'App_Data' folder in my mvc application in case that someone needs this.

     Path.Combine(HttpRuntime.AppDomainAppPath,"App_Data")
    
    0 讨论(0)
  • 2020-11-22 14:27

    For ASP.NET, the Load User Profile setting needs to be set on the app pool but that's not enough. There is a hidden setting named setProfileEnvironment in \Windows\System32\inetsrv\Config\applicationHost.config, which for some reason is turned off by default, instead of on as described in the documentation. You can either change the default or set it on your app pool. All the methods on the Environment class will then return proper values.

    0 讨论(0)
  • 2020-11-22 14:30

    In .net2.0 you can use the variable Application.UserAppDataPath

    0 讨论(0)
  • 2020-11-22 14:34

    The path is different if you're talking ASP.NET.

    I couldn't find any of the 'SpecialFolder' values that pointed to /App_Data for ASP.NET.

    Instead you need to do this:

     HttpContext.Current.ApplicationInstance.Server.MapPath("~/App_Data")  
    

    (Note: You don't need the 'Current' property in an MVC Controller)

    If theres another more 'abstract' way to get to App_Data would love to hear how.

    0 讨论(0)
  • 2020-11-22 14:39

    This is working for me in a console application -

    string appData = System.Environment.GetEnvironmentVariable("APPDATA");
    
    0 讨论(0)
提交回复
热议问题