c# - 在Windows服务应用程序中构建输出文件路径

标签 c# wcf windows-services

我有一个 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/

相关文章:

c# - Visual Studio 一个项目有几个 dll 作为输出?

c# - LINQ 使用列表从数据库中选择最后且唯一的记录

c# - 如何使 Winforms datagridview 扩展列宽以适应内容?

rabbitmq - 如何重启RabbitMQ服务

c# - 我怎样才能自动启动windows服务

c# - 在多线程 Windows 服务应用程序中使用的最佳 NHibernate session 管理方法是什么?

c# - ASP.NET 为 ActionResult 提供参数

wcf - 在WCF服务中HttpContext.Current是否不应该为null?

c# - WCF 传输与消息

WCFExtras XmlComments ...它们在哪里?