1

I'm written a basic application that watches one network File Share directory and when a new file is created in that directory it then fires an external application that parses that file. I've also tested with a local directory and everything worked. I've tested it with debug code like so and the application will work:

#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

Then when I switch to release, build and install the service nothing will happen. So since the path worked i figured this was down to permissions?

I went to Task Manager>Services>Services..> right clicked on my service>Properties>Log On> and have given it my credentials.

I've also gone to the root folder of where my application is on the network Right click>Security>Edit. Then I gave my account Modify, Read & Execute, Listing folder contents, and Read permissions and of course those permissions propagated to all of the folders under its hierarchy.

I even tried mapping it to the network drive Z and trying to access it that one.

With everything I tried the service still refuses to do anything. I've added more debugging code where I would check if the file was changed or deleted and write it down in text files. Once again it would work and detect those changes in debug but upon install nothing would happen.

I'm pretty sure this is probably still some kind of permission issue can anyone tell me what else I could do to remedy this issue?

EDIT:

There was a request for more code. Also note that my Utility class was able to produce a stack trace. It lead to an issue with System.IO.FileStream error started from this line of code in the 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');
             }
Mickey Sly
  • 429
  • 2
  • 6
  • 21

2 Answers2

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.

Justin Harvey
  • 14,446
  • 2
  • 27
  • 30
  • Sorry I'm a little confused by your answer. Are you saying that within the service I need to programmatically check if the service is using the proper user account and if not switch the account credentials? Because under my service's properties under "Log On" it currently has my credentials. – Mickey Sly Feb 07 '14 at 15:33
  • I meant in the service properties, it should be set to run under your account. – Justin Harvey Feb 07 '14 at 15:52
  • Yep. It's running under my account. – Mickey Sly Feb 07 '14 at 17:51
0

I have no idea why you are calling System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite), but I suspect that removing that line will solve your problem. If it doesn't, please post more code.

Ian Kemp
  • 28,293
  • 19
  • 112
  • 138
  • I guess I got some bad information in regards to services. I got that from a tutorial that was supposedly suppose to allow to to run indefinitely. I don't think that was the answer as the problem persists... I will post more code in a couple of mintues. – Mickey Sly Feb 07 '14 at 15:48