Windows Service 无法正常运行 c#

本文关键字:正常运行 Service Windows | 更新日期: 2023-09-27 18:37:23

如果我调试或作为控制台应用程序运行,我的 Windows 服务可以正常运行,但如果我安装,它会显示在服务中,但根本不运行。请帮忙

这是主要功能

static void main(){
if (Environment.UserInteractive)
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "''Service1";
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    //di.Delete(); 
                }


                StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "''Service1''program.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " : Here if'n");
                //     sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : ");
                //    sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : ");
                sw.Flush();
                sw.Close();
                Console.WriteLine("here1");
                Service1 service1 = new Service1();
                string[] args = { "Kun", "Singh" };
                service1.TestStartupAndStop(args);
            }
            else
            {
                string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "''Service1";
                // Determine whether the directory exists.
                if (!Directory.Exists(path))
                {
                    DirectoryInfo di = Directory.CreateDirectory(path);
                    //di.Delete(); 
                }


                StreamWriter sw = new StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "''Service1''program.txt", true);
                sw.WriteLine(DateTime.Now.ToString() + " : Here else'n");
           //     sw.WriteLine("userprofile " + Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + " : ");
            //    sw.WriteLine("application data " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + " : ");
                sw.Flush();
                sw.Close();


                Console.WriteLine("here2");
                // Put the body of your old Main method here.
                ServiceBase[] ServicesToRun;
                ServicesToRun = new ServiceBase[]
                {
                new Service1()
                };
                ServiceBase.Run(ServicesToRun);

                // RunAsync().Wait();
                Service1 myServ = new Service1();
                myServ.write("hello bb");
                Console.Read();
            }

Windows Service 无法正常运行 c#

可能是因为 Environment.UserInteractive 返回 false。您正在运行的服务意味着没有用户界面。请参阅环境.用户交互属性

属性为 Windows 进程或 服务,如在没有用户界面的情况下运行的 IIS。如果此属性 为 false,不显示模式对话框或消息框,因为 没有供用户交互的图形用户界面。

我建议尝试将条件更改为 if (!环境.用户交互)

我刚刚做了一个新的设置 WIX,现在它可以工作,尽管还有第二个问题...... 我的 Windows 服务项目中有一些参考资料,如何在安装和运行时将它们包含在设置中,我有日志文件显示 xxx.dll 丢失

我有以下参考资料 -> jose-jwt.dll ,实体框架。

那么我该如何设置参考文件