如果我作为控制台应用程序进行调试或运行,我的Windows服务将正常运行,但如果安装,它将显示在服务中,但不会运行。请帮助Windows服务无法正常运行c#
这是主要的功能
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();
}
您确定该服务正在运行吗?转到运行并输入Services.msc,按回车键查看所有正在运行的服务。 –
它的运行只有我甚至尝试重新启动....代码很好,大多在调试模式下,所有文件都被写入,并且DB工作...但是如果使用installutil.exe,它会安装但没有任何反应....我不' t什么样的问题,它早些时候运行在示例程序....是否有任何方式调试seeply –
我甚至尝试过 [有条件的(“DEBUG_SERVICE”)] private static void DebugMode() { Debugger.Break(); }其工作在调试模式 –