【问题标题】:FileSystemWatcher Access to a FileShare: Possible permission iusse?FileSystemWatcher 访问文件共享:可能的权限问题?
【发布时间】:2014-02-08 00:10:12
【问题描述】:

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

#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! " + DateTime.Now.ToString());

Service1.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');
             }

【问题讨论】:

    标签: c# filesystemwatcher network-drive fileshare


    【解决方案1】:

    检查服务是否在与您在检查共享或映射驱动器时使用的交互式用户帐户相同的帐户下运行。如果没有,请尝试将其切换为使用此帐户。

    【讨论】:

    • 抱歉,您的回答让我有点困惑。您是说在服务中我需要以编程方式检查服务是否使用正确的用户帐户,如果没有切换帐户凭据?因为在“登录”下我的服务属性下,它当前有我的凭据。
    • 我的意思是在服务属性中,它应该设置为在您的帐户下运行。
    • 是的。它在我的帐户下运行。
    【解决方案2】:

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

    【讨论】:

    • 我想我得到了一些关于服务的坏信息。我是从一个据说可以无限期运行的教程中得到的。我认为这不是答案,因为问题仍然存在......我将在几分钟内发布更多代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 2021-06-18
    • 1970-01-01
    相关资源
    最近更新 更多