How to specify path in .config file relative to the file?

前端 未结 6 2696
清歌不尽
清歌不尽 2021-02-19 18:44

An app I use interprets a .NET .config file. I added a line specifying the path to a certificate it needs



        
6条回答
  •  情话喂你
    2021-02-19 18:53

    you config file may lie on other location then the executing file. as you mantion executing path i understand this is the desktop based application so here you go what you can do like this.

    in Config.

     
            AppPath\MyFile.abc
        
    

    and to retrive this.

       var path = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
    
                if  (path !=null && path.Contains("AppPath"))
                {
    
                    var filepath = System.IO.Path.Combine(
                        System.Reflection.Assembly.GetExecutingAssembly().Location,
                        path.Replace("AppPath", string.Empty).ToString());
    
                    Console.WriteLine(filepath);
                }
    

提交回复
热议问题