C# ASP.NET Web.Config中文件的相对路径

C# ASP.NET Web.Config中文件的相对路径,c#,asp.net,visual-studio,web-config,C#,Asp.net,Visual Studio,Web Config,我想在Web.Config文件中指定应用程序中文件的路径,然后在控制器中调用该路径。 从我在网上发现的情况来看,我大部分时间都在那里 Web.Config <appSettings> <add key="filePath" value= "~/App_Data/Physicians.xml" /> </appSettings> 然而,这指向了错误的位置 如何从应用程序的根目录开始,将其指向存储在App_Data文件夹中的文件?您可以使用Server

我想在Web.Config文件中指定应用程序中文件的路径,然后在控制器中调用该路径。 从我在网上发现的情况来看,我大部分时间都在那里

Web.Config

<appSettings>
    <add key="filePath" value= "~/App_Data/Physicians.xml" />
</appSettings>
然而,这指向了错误的位置


如何从应用程序的根目录开始,将其指向存储在App_Data文件夹中的文件?

您可以使用
Server.MapPath

或者,在配置文件中仅存储相对路径,然后使用:

<appSettings>
    <add key="filePath" value= "App_Data/Physicians.xml" />
</appSettings>

string relativePath = ConfigurationManager.AppSettings["filePath"];
string combinedPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)

string relativePath=ConfigurationManager.AppSettings[“文件路径”];
字符串combinedPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,relativePath)

后一种技术适用于非web应用程序,因此可以说更好。

您介意再详细说明一下吗?你是说我不去管Web.Config部分吗?你能写出你将如何在控制器中打这个电话吗?我不知道如何使用您的路径。请将示例与我拥有的结合起来,或者如果我要替换所有这些。relativePath将是您存储在Web.Config中的部分。您可以从路径中删除tilda,然后执行类似var relativePath=ConfigurationManager.AppSettings[“filePath”].ToString()的操作,然后使用path.Combine(AppDomain.CurrentDomain.BaseDirectory,relativePath)我知道| DataDirectory |在连接字符串中起作用。在AppSettings中不确定。
<appSettings>
    <add key="filePath" value= "App_Data/Physicians.xml" />
</appSettings>

string relativePath = ConfigurationManager.AppSettings["filePath"];
string combinedPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath)