向 Web 服务添加内容文件和从 Web 服务访问内容文件

Adding content files to and accessing content files from a web service

我想在我的网络 application/web 服务的 bin 目录中包含一个 XML 文件。我在 Visual Studio 的项目中包含了 bin 文件夹并添加了文件。我将其构建操作设置为 "Content" 并将复制到输出目录设置为 "Copy Always"。当我的代码去寻找它时,我使用

string dir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
if (!dir.EndsWith(@"\")) dir += @"\";

获取目录 return 为 appPath,其中 appPath 是(非转义版本):

C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vs[=11=]c7655f\d4908928\assembly\dl3cf9fd67\fdc52284_ffa7d201\

然后我附加 XML 文件的文件名(其中 string filename = "myXmlFile.xml")以读取它:

StreamReader reader = new StreamReader(appPath + filename);

但是我在那行代码中遇到异常,它找不到我的文件。我正在处理转义斜杠的问题,所以不是那样的。当我检查路径指向的物理目录时,它没有复制到那个目录,这就是异常的原因。那么如何让我的文件复制到那里呢?

如何使用HostingEnvironment.ApplicationPhysicalPath

var dir = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, "bin\filename.xml")

参见相关问题how to get the application path in asp.net?