如果我作爲控制檯應用程序進行調試或運行,我的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(); }其工作在調試模式 –