當類庫項目中無法使用Application.StartupPath

來源:互聯網
上載者:User

標籤:

通常我們WinForm編程時,要擷取程式當前啟動並執行檔案夾路徑會用Application.StartupPath ,但是Application.StartupPath在編寫類庫項目時卻無法使用,因為我們根本無法用using System.Windows.Forms;來引入Application.StartupPath 的命名空間,這個時侯我們要用AppDomain.CurrentDomain.BaseDirectory。

private static string fullPathFileName = Application.StartupPath + "\\Set.Ini"; //用於WINFORM
private static string fullPathFileName = AppDomain.CurrentDomain.BaseDirectory+"\\Set.Ini"; //用於類項目

 

using System.Windows.Forms;

控制台程式添加System.Windows.Forms;

因為是控制台應用程式,還要去添加引用

//擷取了應用程式路徑,不包括可執行檔名稱。   

  1. string path1 = Application.StartupPath;  
  2. Console.WriteLine(path1);  
  3. //擷取啟動了應用程式的可執行檔的路徑,包括可執行檔的名稱。      
  4. string path2 = Application.ExecutablePath;  
  5. Console.WriteLine(path2);  
  6. //substring()用法   
  7. //取得執行檔案根目錄   
  8. path1 = path1.Substring(0,path1.LastIndexOf("\\"));  
  9. Console.WriteLine(path1);  
  10. Console.ReadLine();  

當類庫項目中無法使用Application.StartupPath

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.