[英]C# How to get the User AppData folder, NOT AppData\Roaming?
[英]How can I create a folder in the app folder in the user's AppData folder e.g. C:\User\AppData?
我試過這個:
var systemPath = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
var complete = Path.Combine(systemPath, extractfilename);
但結果是:
C:\\ProgramData\\Extract.txt
我的預期輸出是:
C:\\User\\AppData\\Extract.txt
您需要在Environment.SpecialFolder.ApplicationData
文件夾中創建文件。 還有另一種方法可以獲取它的值,因此您也可以使用它並附加路徑。
例如
string path;
path = @"%AppData%\test";
Environment.ExpandEnvironmentVariables(path);
C:\\User\\AppData\\Local
使用Environment.SpecialFolder.LocalApplicationData
:
用作當前非漫游用戶使用的特定於應用程序的數據的公共存儲庫的目錄。
C:\\User\\AppData\\Roaming
使用Environment.SpecialFolder.ApplicationData
:
用作當前漫游用戶的應用程序特定數據的公共存儲庫的目錄。 漫游用戶在網絡上的多台計算機上工作。 漫游用戶的配置文件保存在網絡上的服務器上,並在用戶登錄時加載到系統上。
這將在“%appdata%”中創建一個名為“MyName”的文件夾。
string directoryName = "MyName";
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string mainPath = Path.Combine(appDataPath, directoryName);
Directory.CreateDirectory(mainPath);
這將在“%appdata%”中創建一個名為“MyFile.txt”的文件,上面寫着“Hello World”。
string text = "Hello Word";
string fileName = "MyFile.txt";
string appDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string mainPath = Path.Combine(appDataPath, fileName);
File.WriteAllText(mainPath, text);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.