1

我编写了一个监视一个网络文件共享目录的基本应用程序,当在该目录中创建一个新文件时,它会触发一个解析该文件的外部应用程序。我还使用本地目录进行了测试,一切正常。我已经用这样的调试代码对其进行了测试,并且该应用程序将正常工作:

#if DEBUG
  Service1 mysService1 = new Service1();
  mysService1.OnDebug(); //calls onStart(Null);
  System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

#else
  ServiceBase[] ServicesToRun;
  ServicesToRun = new ServiceBase[] 
  { 
    new Service1() 
  };

  ServiceBase.Run(ServicesToRun);
  System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#endif

然后,当我切换到发布、构建和安装服务时,什么都不会发生。因此,由于路径有效,我认为这取决于权限?

我去Task Manager>Services>Services..>右键单击我service>Properties>Log On>并给它我的凭据。

我还去了我的应用程序在网络上的根文件夹Right click>Security>Edit。然后我给了我的帐户修改、读取和执行、列出文件夹内容和读取权限,当然这些权限会传播到其层次结构下的所有文件夹。

我什至尝试将它映射到网络驱动器 Z 并尝试访问它。

尽管我尝试了一切,但该服务仍然拒绝做任何事情。我添加了更多调试代码,我将在其中检查文件是否已更改或删除并将其写在文本文件中。它会再次工作并在调试中检测到这些更改,但在安装时不会发生任何事情。

我很确定这可能仍然是某种许可问题,谁能告诉我我还能做些什么来解决这个问题?

编辑:

有人要求提供更多代码。另请注意,我的 Utility 类能够生成堆栈跟踪。它导致 System.IO.FileStream 错误从 FileWatcher.cs System.IO.File.AppendAllText(PathLocation() + "\logFile.txt", Environment.NewLine + " Started! " + 日期时间.Now.ToString());

服务1.cs:

 public Service1()
    {
        InitializeComponent();
    }
    public void OnDebug()
    {
        OnStart(null);
    }

    protected override void OnStart(string[] args)
    {
        try
        {

            FileWatcher f = new FileWatcher();

        }
        catch (Exception e)
        {
            new ErrorMailer(e, DateTime.Now.ToString());

        }

    }

FileWatcher.cs:

        private FileSystemWatcher _fileWatcher;
     static ProcessStartInfo start;

      public FileWatcher()
               {
   System.IO.File.AppendAllText(PathLocation() + "\\logFile.txt",   Environment.NewLine + " Started! " + DateTime.Now.ToString());
       _fileWatcher = new FileSystemWatcher(PathLocation());
                           HasMailClerkBeenRun = false;
                           start = new ProcessStartInfo();
                           _fileWatcher.Created += new FileSystemEventHandler(_fileWatcher_Created);
                           _fileWatcher.Deleted += new FileSystemEventHandler(_fileWatcher_Deleted);
                           _fileWatcher.Changed += new FileSystemEventHandler(_fileWatcher_Changed);
       _fileWatcher.EnableRaisingEvents = true;
    }

    {
                string value = String.Empty;


                value = @"Z:\MyAppDirectory\DirectoryFileWatcherIsWatching"; //@"\\FileShareName\RootDirectory\MyAppDirectory\DirectoryFileWatcherIsWatching";


                return value;

            }

     void _fileWatcher_Changed(object sender, FileSystemEventArgs e)
             {

                 System.IO.File.AppendAllText(PathLocation() + "\\logFile.txt", Environment.NewLine + "Started from the bottom now we changed! " + DateTime.Now.ToString());
             }

             void _fileWatcher_Deleted(object sender, FileSystemEventArgs e)
             {
                 System.IO.File.AppendAllText(PathLocation() + "\\logFile.txt", Environment.NewLine + "Started from the bottom now we deleted! " + DateTime.Now.ToString());

             }

             void _fileWatcher_Created(object sender, FileSystemEventArgs e)
             {


                 System.IO.File.AppendAllText(PathLocation() + "\\logFile.txt", Environment.NewLine + "Started from the bottom now we here! " + DateTime.Now.ToString());

                 LaunchExternalApp();
             }

     private void LaunchExternalApp()
             {
        start.UseShellExecute = false;
                         start.RedirectStandardError = true;
                         start.RedirectStandardInput = true;
                         start.RedirectStandardOutput = true;
                         start.CreateNoWindow = true;
                         start.ErrorDialog = false;
                         start.WindowStyle = ProcessWindowStyle.Hidden;
     start.FileName =@"Z:\MyAppDirectory\AppExcutionLocation\MyApp.exe"

     Thread thread1 = new Thread(new ThreadStart(A));
                               thread1.Start();
                               thread1.Join();
                }

       static void A()
             {

                 using (Process proc = Process.Start(start))
                 {
                     proc.WaitForExit();
                     //HasMailClerkBeenRun = true;
                     // Retrieve the app's exit code
                     ///   int exitCode = proc.ExitCode;
                 }
                 Thread.Sleep(100);
                 Console.WriteLine('A');
             }
4

2 回答 2

0

Check that the service is running under the same account as the interactive user account that you are using when checking the share or mapping the drive. If not, try switching it to use this account.

于 2014-02-07T15:24:01.253 回答
0

我不知道你为什么打电话System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite),但我怀疑删除那条线会解决你的问题。如果没有,请发布更多代码。

于 2014-02-07T15:28:38.147 回答