簡體   English   中英

如何在C#.net的Windows應用程序根目錄中讀取文件?

[英]How to read file in windows application root directory in C#.net?

如果我使用Application.StartupPath或AppDomain.CurrentDomain.BaseDirectory它在bin \\ debug文件夾中搜索該文件,但是我必須在ma c#.net項目中使用我的根目錄“Resources \\ imagefile.png”中的文件夾!

Filestream fs;
fs = new Filestream(AppDomain.CurrentDomain.BaseDirectory + "folder\\imagefile.png");

那么,如果使用上面的代碼或“@C:.......”目錄的完整路徑,如何編寫代碼從我的根目錄讀取文件,甚至Server.MapPath我們也無法使用。

要獲取我的應用程序路徑,但這會產生類似C:\\ Projects \\ XYZ \\ ABC \\ bin \\ Debug的內容

我不想bin \\ Debug。有沒有辦法實現這個?

通常,您將這些項目設置為要復制到bin文件夾中。 右鍵單擊解決方案資源管理器/導航器,選擇屬性並將“復制到輸出目錄”設置為不同的東西。

通過使用Environment.CurrentDirectory

string yourpath = Environment.CurrentDirectory + @"\YourImage.jpg";
FileStream stream = new FileStream(yourpath, FileMode.Open, FileAccess.Read);
Image image = Image.FromStream(stream);
stream.Close(); 

暫無
暫無

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

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