簡體   English   中英

如何找到WPF應用程序的根路徑並在根目錄上創建文件夾以保存圖像?

[英]How to find root path of wpf app and create folder on root directory for saving images?

我有一個名為osc的應用程序,我想在根路徑中創建文件夾並將圖像保存到該文件夾​​。

我想將圖像保存在“ osc / Screenshots / EmpName / EmpId / scr.jpg”文件夾中

如何在此路徑上創建文件夾並保存圖像

幫我

提前致謝

要獲取表示路徑的字符串:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

可能您想執行以下操作:

DirectoryInfo directory = new DirectoryInfo(path);

要基於根文件夾創建新路徑:

var imagePath = path + @"\MyAppName\Images"

if (!Directory.Exists(imagePath)) 
{   
   DirectoryInfo di = Directory.CreateDirectory(imagePath);
}

編輯:作為替代,您可以使用特殊文件夾。 例如LocalApplicationData,在此文件夾中,您可以按用戶放置應用程序的計算機范圍數據。 在變量localData下將是

C:\\ Users \\ USER_NAME \\ AppData \\ Local

其中USER_NAME是用戶個人資料。

string localData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var imagePath = localData + @"\Images";

if (!Directory.Exists(imagePath)) 
{   
    DirectoryInfo di = Directory.CreateDirectory(imagePath);
}

AppDomain.CurrentDomain.BaseDirectory將為您提供應用程序文件夾的路徑。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM