简体   繁体   English

在Windows Service System中安装后,Windows Service无法写入文本文件

[英]Windows Service not writing text file after installation in Windows Service System

I have made a windows Service that contains a Socket Connection Code.As per the code after installation of this windows Service and after starting,it should write some Messages into the text file but i am not able to see any of my messages into text file.While in my local PC which contains Windows 7 ,my this service is able to write into the text file. 我已经制作了一个包含套接字连接代码的Windows服务。按照此Windows服务安装后并启动后的代码,它应该将一些消息写入文本文件,但是我看不到任何消息到文本文件。虽然在包含Windows 7的本地PC上,我的这项服务仍能够写入文本文件。 Here is my code.. 这是我的代码。

            try
            {
                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Before Connection !");
                }

                client.Connect(new IPEndPoint(IPAddress.Parse(ip), intport));

                Thread.Sleep(5000);

                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Connected To Machine !");
                }

            }
           catch (Exception ex)
            {
                // Log the error here.
                client.Close();

                using (StreamWriter writer = new StreamWriter(textfileSaveLocation, true))
                {
                    writer.WriteLine("Connection Error !");
                }

                continue;
            }

So my doubt is why is my Service not able to write into Server text file while it is doing in my local System.Also there are other text files in the server PC that is getting messages from other Windows Services made from someone else.Why is this happening in my case? 所以我的疑问是为什么我的服务在本地系统中执行服务时无法写入服务器文本文件,服务器PC中还有其他文本文件正在从其他人获得的其他Windows服务中获取消息。以我为例 Please help me. 请帮我。 Thanks in advance. 提前致谢。

Check the following: 检查以下内容:

  1. The account running the windows service has write permissions on the folder path. 运行Windows服务的帐户对文件夹路径具有写权限。
  2. The folder path is correct. 文件夹路径正确。
  3. You might be running the service as Admin on your local box, hence it has all privileges. 您可能在本地机器上以管理员身份运行该服务,因此它具有所有特权。
  4. Also in your Exception block, ensure the ex.ToString() is logged. 同样在您的Exception块中,确保已记录ex.ToString()。 It gives a lot of info. 它提供了很多信息。
  5. Your client.Close() can also raise an exception. 您的client.Close()也可能引发异常。 Take care of that scenario. 注意这种情况。

If there are file access issues, then put an uber try/catch block and try to write the text file on a public folder first where you know Everyone has access. 如果存在文件访问问题,请放置uber try / catch块,然后尝试将文本文件首先写入您知道每个人都可以访问的公用文件夹。

In serviceProcessInstaller you should set the Account property to LocalSystem . serviceProcessInstaller中 ,应将Account属性设置为LocalSystem This will cause the service to be installed and to run on a local service account. 这将导致该服务被安装并在本地服务帐户上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM