简体   繁体   English

代码在控制台应用程序中有效,但在Windows Service中运行时不起作用

[英]Code works in a console application, but does not work when running in windows service

I wrote a windows service. 我写了一个Windows服务。 Part of the code works : (example from msdn) 部分代码起作用:(来自msdn的示例)

protected override void OnStart(string[] args)
    {
        file = new StreamWriter(new FileStream("MyFirstService.log",
                          System.IO.FileMode.Append));
        this.file.WriteLine("MyFirstService стартовал");
        this.file.Flush(); 
        Thread thread = new Thread(new ThreadStart(WatchProcess));
        thread.Start();
    }

I am adding this code : 我正在添加此代码:

private void WatchProcess()
    {
            var key = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true);
            key.SetValue("dWatch", System.Reflection.Assembly.GetEntryAssembly().Location);
            System.IO.FileSystemWatcher fWatcher;
            fWatcher = new System.IO.FileSystemWatcher();
            fWatcher.IncludeSubdirectories = true;
            fWatcher.Path = @"C:\Windows";
            fWatcher.Filter = "*.*";
            fWatcher.NotifyFilter = System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.DirectoryName;
            fWatcher.Changed += new FileSystemEventHandler(OnChanged);
            fWatcher.Created += new FileSystemEventHandler(OnChanged);
            fWatcher.Deleted += new FileSystemEventHandler(OnChanged);
            fWatcher.Renamed += new RenamedEventHandler(OnRenamed);
            fWatcher.EnableRaisingEvents = true;
    }

    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        WatcherChangeTypes wtc = e.ChangeType;
        string str = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
        StreamWriter sw = new StreamWriter(str);
        Console.WriteLine("File: " + e.FullPath + " " + wtc.ToString());
    }


    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        string str = System.Environment.GetFolderPath(System.Environment.SpecialFolder.UserProfile);
        StreamWriter sw = new StreamWriter(str);
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }

The code above works when I run it in console application, but in windows service it does not work. 当我在控制台应用程序中运行它时,上面的代码有效,但是在Windows服务中却不起作用。 And service automatically stops after 30 seconds. 30秒后,服务会自动停止。

 protected override void OnStart(string[] args)
    {
        file = new StreamWriter("C:\\Public\\fswThread." + DateTime.Now.Millisecond.ToString() + ".txt");
        System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher();
        watcher.Path = @"C:\Windows";
        watcher.Filter = "*.*";
        watcher.IncludeSubdirectories = true;
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
                             | NotifyFilters.FileName   | NotifyFilters.DirectoryName;
        watcher.Changed += new FileSystemEventHandler( OnChanged );
        watcher.EnableRaisingEvents = true;
    }

    protected override void OnStop()
    {
        file.Close();
    }

    private void OnChanged(object sender, FileSystemEventArgs e)
    {
        file.WriteLine( "C: " + e.FullPath );
        file.AutoFlush = true;

        if (file.BaseStream.Length > 100)
        {
            file.Flush();
            file.Close();
            file = new StreamWriter("C:\\Public\\fswThread." + DateTime.Now.Millisecond.ToString() + ".txt");
        }


    }

暂无
暂无

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

相关问题 从Windows服务和控制台应用程序访问相同的运行代码? - Access the same running code from a Windows Service and a console application? 运行 C# 控制台应用程序作为 Windows 服务 - Running a C# console application as a Windows service 运行批处理文件的C#代码在控制台应用程序中工作,但相同的代码在WCF服务中不起作用 - C# code to run a batch file works in console application, but same code doesnt work in WCF Service 作为Windows服务运行时,MSMQ应用程序不处理排队的消息 - MSMQ Application does not process queued messages when running as Windows Service 代码在控制台应用程序中运行,但不在Windows通用应用程序中运行 - Code running in console application but not windows universal app XpsDocumenWriter从Windows Service挂起,但是从控制台运行时可以正常工作 - XpsDocumenWriter hangs from Windows Service, but works fine when running from console Windows 服务给出的字符串未被识别为有效的 DateTime 异常,但相同的代码在控制台应用程序中正常运行 - Windows Service gives String was not recognized as a valid DateTime exception but the same code running properly in Console Application 在控制台中运行Windows服务 - Running a windows service in a console C#:。Net:用于控制台应用程序的代码。 在WCF服务上运行时,相同的代码失败 - C# : .Net : Code working on Console application . Same code is failing when running on WCF service 一段代码在控制台应用程序中工作,但在nunit测试中不起作用 - piece of code works in console application, but does not works inside nunit test
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM