代码在控制台应用程序中工作,但在Windows服务中运行时不起作用

时间:2014-01-27 04:56:15

标签: c# visual-studio-2012 windows-services

我写了一个Windows服务。部分代码可以工作:(来自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();
    }

我正在添加此代码:

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);
    }

上面的代码在我在控制台应用程序中运行时起作用,但在Windows服务中它不起作用。服务会在30秒后自动停止。

1 个答案:

答案 0 :(得分:0)

 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");
        }


    }