C# Windows服务出现错误1053-System.IO.DirectoryNotFoundException

C# Windows服务出现错误1053-System.IO.DirectoryNotFoundException,c#,exception,windows-services,C#,Exception,Windows Services,我有一个小型控制台应用程序,其中包含一个用c#编写的web服务器。尝试将其转换为windows服务时,出现错误1053,查看错误日志时显示: Application: YCSWebServerService.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.DirectoryNotFou

我有一个小型控制台应用程序,其中包含一个用c#编写的web服务器。尝试将其转换为windows服务时,出现错误1053,查看错误日志时显示:

Application: YCSWebServerService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.DirectoryNotFoundException
Stack:
   at HttpServer.Resources.FileResources.Add(System.String, System.String)
   at HttpServer.Resources.FileResources..ctor(System.String, System.String)
   at YCSWebServer.WebServer..ctor(SerialCommunication.SerialCommunicationWrapper, YCSInterfaces.IBuilder, SerialCommunication.SerialCommunication)
   at YCSConfiguration.Builder..ctor()
   at YCSWebServerService.YCSWebServerService..ctor()
   at YCSWebServerService.Program.Main()
这是由于以下代码造成的:

server = new Server();

// Where to find the html page to render
server.Resources.Add(new FileResources("/", ".\\Webpages"));
server.Add(new FileModule(server.Resources, false));

//Create a http listener
HttpListener listener = HttpListener.Create(IPAddress.Any, 8888);

// use one http listener.
server.Add(listener);

server.RequestReceived += ServerRequestReceived;

// start server, can have max 10 pending accepts.
server.Start(10);
我尝试设置html文件所在位置的相对路径,并尝试为其提供固定路径fx:c:\webpages,但没有成功。我总是犯同样的错误。 我是否设置了某种权限/安全性,以便我的服务访问此文件夹?? Web服务器基于codeplex项目:

我的onstart和onstop方法:

public partial class YCSWebServerService : ServiceBase
    {
        private Builder _builder;
        public YCSWebServerService()
        {
            InitializeComponent();
            _builder = new Builder();
        }

        protected override void OnStart(string[] args)
        {
            _builder.Start();
        }

        protected override void OnStop()
        {
            _builder.Stop();
        }
    }

在服务器上,如果执行启动->运行,键入
services.msc
(在Server2008上,只需启动->键入
services.msc
)并按enter键,您将获得服务列表。在列表中找到您的并查看属性。如果转到“登录”选项卡,您将看到服务配置为的用户。若它是为本地系统设置的,那个么您就不必担心文件/目录的安全权限。如果设置为其他内容,请确保帐户有权访问文件/目录。

在服务器上,如果确实启动->运行,请键入
services.msc
(在服务器2008上,只需启动->键入
services.msc
),然后按enter键,您将获得服务列表。在列表中找到您的并查看属性。如果转到“登录”选项卡,您将看到服务配置为的用户。若它是为本地系统设置的,那个么您就不必担心文件/目录的安全权限。如果设置为其他内容,请确保该帐户有权访问文件/目录。

大约一周前,我也遇到过同样的问题。问题是windows服务没有“启动路径”设置,因此解决方案中文件的相对路径将不起作用

此函数获取服务正在其中执行的文件夹,您需要将其预先添加到
“\\Webpages”
,而不要使用
\\Webpages”

希望这有帮助

private static string ExecutingFolder()
{
    string exeUri = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    Uri uri = new Uri(exeUri);
    string physicalExePath = uri.LocalPath;
    return IO.Path.GetDirectoryName(physicalExePath);
}

大约一周前,我遇到了完全相同的问题。问题是windows服务没有“启动路径”设置,因此解决方案中文件的相对路径不起作用

此函数获取服务正在其中执行的文件夹,您需要将其预先添加到
“\\Webpages”
,而不要使用
\\Webpages”

希望这有帮助

private static string ExecutingFolder()
{
    string exeUri = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
    Uri uri = new Uri(exeUri);
    string physicalExePath = uri.LocalPath;
    return IO.Path.GetDirectoryName(physicalExePath);
}