【轉】c#.net各種應用程序中獲取文件路徑的方法


控制台應用程序:Environment.CurrentDirectory、Directory.GetCurrentDirectory()

windows服務:Environment.CurrentDirectory

windows服務安裝成功后:

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

2. ///

/// 獲取服務應用程序的安裝路徑(或者當前安裝目錄)///

 

/// /// public static string GetWindowsServiceInstallPath(string ServiceName)

 

{

 

string key = @"SYSTEM\CurrentControlSet\Services\" + ServiceName;

 

string path = Registry.LocalMachine.OpenSubKey(key).GetValue("ImagePath").ToString();

 

//替換掉雙引號 

 

path = path.Replace("\"", string.Empty);

 

FileInfo fi = new FileInfo(path);

 

return fi.FullName;

 

//return fi.FullName.Directory.ToString();

 

}

 

//windows 服務中使用log4net

string assemblyFilePath = Assembly.GetExecutingAssembly().Location; 

 string assemblyDirPath = Path.GetDirectoryName(assemblyFilePath); 

 string configFilePath = assemblyDirPath + "//log4net.config"; 

 DOMConfigurator.ConfigureAndWatch(new FileInfo(configFilePath));

 

      /// <summary>
        /// 獲取應用程序web.config中的文件配置路徑,並返回物理路徑
        /// 適用於web應用程序
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetFileFullpath(string key)
        {
            if (string.IsNullOrEmpty(key)) return string.Empty;


            //獲取應用程序的web.config中配置的路徑
            string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
            //如果到的路徑不是物理路徑,則映射為物理路徑
            if (!Path.IsPathRooted(appSetting)) appSetting = System.Web.HttpContext.Current.Server.MapPath(appSetting);


            return appSetting;
        }

 

/// <summary>
        /// 獲取應用程序.config中的文件配置路徑,並返回物理路徑
        /// 適用於windows服務、控制台等應用程序
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetAssemblyPath(string key)
        {
            if (string.IsNullOrEmpty(key)) return string.Empty;


            //獲取應用程序的web.config中配置的路徑
            string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
            //如果到的路徑不是物理路徑,則映射為物理路徑
            if (!Path.IsPathRooted(appSetting))
            {
                string assemblyPath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                string dirName = Path.GetDirectoryName(assemblyPath);
                if (dirName.IndexOf(@"\bin\Debug") > -1)
                    appSetting = dirName.Replace(@"\bin\Debug", appSetting.Substring(1).Replace(@"/", @"\"));
                else
                    appSetting = dirName + appSetting.Substring(1).Replace(@"/", @"\");
            }


            return appSetting;
        }

 

  /// <summary>
        /// 獲取應用程序.config中的文件配置路徑,並返回物理路徑
        /// 適用於windows服務應用程序的成功安裝之后
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetInstallPath(string key)
        {
            if (string.IsNullOrEmpty(key)) return string.Empty;

            //獲取應用程序的web.config中配置的路徑
            string appSetting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
            //如果到的路徑不是物理路徑,則映射為物理路徑
            if (!Path.IsPathRooted(appSetting))
            {
                string processPath = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
                appSetting = processPath.Substring(0, processPath.LastIndexOf(@"\")) + appSetting.Substring(1).Replace(@"/", @"\");
            }


            return appSetting;
        }

 

 

 

轉載:http://blog.csdn.net/cafuc229/article/details/7667172


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM