2014-04-04 90 views
0

我正在寫一個類庫,並將其引用到WCF服務中。如何在類庫中獲取文件的完整路徑?

在這個類庫中,我需要獲取app.config文件的物理路徑。

它正在使用完整的物理路徑進行硬編碼時工作。但是,我不想這樣做。

請看看我的代碼:

private static void loadConfig() 
     { 
      strConfigpath = "app.config"; 
      log4net.Config.XmlConfigurator.Configure(new FileInfo(Path.GetFullPath(strConfigpath))); 
     } 

我嘗試使用Path.GetFullPath()。但是,它給錯誤的結果。

我不能使用Server.MapPath(),因爲它不是網絡服務網絡應用程序。

如何做到這一點?螞蟻的想法或建議?

回答

0

使用FullName屬性

string fileName = "app.config"; 
    FileInfo f = new FileInfo(fileName); 
    string fullname = f.FullName; 
    f.DirectoryName; //This will give the folder path which is having the file 

嘗試這樣的事情,

string folder = System.Web.HttpContext.Current != null ? 
System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_data") :   System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); 

refered:Get the file path of current application's config file

+0

@ Sajeetharan..Its越來越走錯了路 –

+0

你能告訴我你的原件和此代碼返回的路徑 – Sajeetharan

+0

C:\ Users \ 343434 \ Desktop \ Final \ MyWCFService \ Utilities \ app.config –

相關問題