我有一个 WCF 服务,可以在创建文件时读取文件。
我将该文件添加到我的项目中,将其Buid Action设置为内容,并将复制到输出目录设置为始终复制,并使用以下命令获取文件路径:
private const string MEDIA_PLAYER_FILENAME = "mediaPlayers.xml";
var mediaPlayerFilePath = Path.Combine(Environment.CurrentDirectory, MEDIA_PLAYER_FILENAME);
当我在控制台应用程序中自行托管它时,效果很好,但是当它托管在 Windows 服务中时,它会失败,因为它尝试从 C:\Windows\system32\mediaPlayers.xml< 读取它/strong>
我想 Environment.CurrentDirectory
不是我应该使用的。应该是什么?或者还有其他方法可以做到这一点吗?
最佳答案
您想要的是执行程序集的路径。 CurrentDirectory 不是同一回事。
请参阅这篇文章以获取该信息:
Getting the path of the current assembly
重新发布:
(new System.Uri(Assembly.GetExecutingAssembly().CodeBase)).AbsolutePath;
或者
Assembly.GetExecutingAssembly().Location
关于c# - 在Windows服务应用程序中构建输出文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21291916/