Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. "The path is not of a legal form." on trying to start the service created in c#

"The path is not of a legal form." on trying to start the service created in c#

Scheduled Pinned Locked Moved C#
csharphelp
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sri_3464 0
    wrote on last edited by
    #1

    Hi, I tried created a windows service which does a file transfer from the folder its watching whenever a new file being created in it.I am getting an exception saying "The path is not of a legal form." on trying to start the service. An the path the exception refering to is the path that i set to the FileSystemWatch. Path property. I dont understand why it happens. The path is "D:\Test". The folder has all the permission to access. Can anyone help me out.

    R K 2 Replies Last reply
    0
    • S sri_3464 0

      Hi, I tried created a windows service which does a file transfer from the folder its watching whenever a new file being created in it.I am getting an exception saying "The path is not of a legal form." on trying to start the service. An the path the exception refering to is the path that i set to the FileSystemWatch. Path property. I dont understand why it happens. The path is "D:\Test". The folder has all the permission to access. Can anyone help me out.

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      When setting your path did you use an @ ?

      string path = @"D:\Test";

      Without that, that \T will likely get converted to a tab which would not be a legal form.

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • S sri_3464 0

        Hi, I tried created a windows service which does a file transfer from the folder its watching whenever a new file being created in it.I am getting an exception saying "The path is not of a legal form." on trying to start the service. An the path the exception refering to is the path that i set to the FileSystemWatch. Path property. I dont understand why it happens. The path is "D:\Test". The folder has all the permission to access. Can anyone help me out.

        K Offline
        K Offline
        Keith Barrow
        wrote on last edited by
        #3

        Without the code it is difficult to help, can post it please? Remember to use <pre> tags :-)!

        CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

        S 1 Reply Last reply
        0
        • K Keith Barrow

          Without the code it is difficult to help, can post it please? Remember to use <pre> tags :-)!

          CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

          S Offline
          S Offline
          sri_3464 0
          wrote on last edited by
          #4

          Service1.cs

          using System;
          using System.Collections.Generic;
          using System.ComponentModel;
          using System.Data;
          using System.Diagnostics;
          using System.Linq;
          using System.ServiceProcess;
          using System.Text;
          using System.Configuration;

          namespace FileTransfer
          {
          public partial class Service1 : ServiceBase
          {
          public Service1()
          {
          InitializeComponent();
          }

              protected override void OnStart(string\[\] args)
              {
                  try
                  {
                      FileTransferWatcher.Created += new System.IO.FileSystemEventHandler(FileTransferWatcher\_Created);
                  }
                  catch (Exception ex)
                  {
                      EventLog.WriteEntry("Message :  " + ex.Message);
                  }
              }
          
              void FileTransferWatcher\_Created(object sender, System.IO.FileSystemEventArgs e)
              {
                  System.IO.File.Copy(e.FullPath, @"D:\\");
              }
          
              protected override void OnStop()
              {
              }
          }
          

          }

          Service1.Designer.cs

          namespace FileTransfer
          {
          partial class Service1
          {
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.IContainer components;

              /// <summary>
              /// Clean up any resources being used.
              /// </summary>
              /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
              protected override void Dispose(bool disposing)
              {
                  if (disposing && (components != null))
                  {
                      components.Dispose();
                  }
                  base.Dispose(disposing);
              }
          
              #region Component Designer generated code
          
              /// <summary> 
              /// Required method for Designer support - do not modify 
              /// the contents of this method with the code editor.
              /// </summary>
              private void InitializeComponent()
              {
                  this.FileTransferWatcher = new System.IO.FileSystemWatcher();
                  // 
                  // FileTransferWatcher
                  // 
                  this.FileTransferWatcher.EnableRaisingEvents = true;
                  **this.FileTransferWatcher.Path = "D:\\\\Test\\\\";**
                  // 
                  // Service1
                  // 
                  this.ServiceName = "FileTransfer";
          
              }
          
              #endregion
          
              private System.IO.FileSystemWatcher FileTra
          
          K 0 T 3 Replies Last reply
          0
          • S sri_3464 0

            Service1.cs

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Diagnostics;
            using System.Linq;
            using System.ServiceProcess;
            using System.Text;
            using System.Configuration;

            namespace FileTransfer
            {
            public partial class Service1 : ServiceBase
            {
            public Service1()
            {
            InitializeComponent();
            }

                protected override void OnStart(string\[\] args)
                {
                    try
                    {
                        FileTransferWatcher.Created += new System.IO.FileSystemEventHandler(FileTransferWatcher\_Created);
                    }
                    catch (Exception ex)
                    {
                        EventLog.WriteEntry("Message :  " + ex.Message);
                    }
                }
            
                void FileTransferWatcher\_Created(object sender, System.IO.FileSystemEventArgs e)
                {
                    System.IO.File.Copy(e.FullPath, @"D:\\");
                }
            
                protected override void OnStop()
                {
                }
            }
            

            }

            Service1.Designer.cs

            namespace FileTransfer
            {
            partial class Service1
            {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components;

                /// <summary>
                /// Clean up any resources being used.
                /// </summary>
                /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
                protected override void Dispose(bool disposing)
                {
                    if (disposing && (components != null))
                    {
                        components.Dispose();
                    }
                    base.Dispose(disposing);
                }
            
                #region Component Designer generated code
            
                /// <summary> 
                /// Required method for Designer support - do not modify 
                /// the contents of this method with the code editor.
                /// </summary>
                private void InitializeComponent()
                {
                    this.FileTransferWatcher = new System.IO.FileSystemWatcher();
                    // 
                    // FileTransferWatcher
                    // 
                    this.FileTransferWatcher.EnableRaisingEvents = true;
                    **this.FileTransferWatcher.Path = "D:\\\\Test\\\\";**
                    // 
                    // Service1
                    // 
                    this.ServiceName = "FileTransfer";
            
                }
            
                #endregion
            
                private System.IO.FileSystemWatcher FileTra
            
            K Offline
            K Offline
            Keith Barrow
            wrote on last edited by
            #5

            You need to set this.FileTransferWatcher.Path = "D:\\Test\\"; before this FileTransferWatcher.EnableRaisingEvents = true; As the inital path of a FileSystemWatcher is an empty string. Also, as a side note, hardcoding the paths in magic strings isn't a good plan (though obviously the code posted is, by definition, unfinished). I'd also move out the FileWatcher/Transfer code into a separate class that deals with this and let the service just handle the service related stuff. The other thing is, if you can, I'd run this as a console app and then convert it into a service later as debugging services is trickier.

            CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

            S 1 Reply Last reply
            0
            • S sri_3464 0

              Service1.cs

              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Diagnostics;
              using System.Linq;
              using System.ServiceProcess;
              using System.Text;
              using System.Configuration;

              namespace FileTransfer
              {
              public partial class Service1 : ServiceBase
              {
              public Service1()
              {
              InitializeComponent();
              }

                  protected override void OnStart(string\[\] args)
                  {
                      try
                      {
                          FileTransferWatcher.Created += new System.IO.FileSystemEventHandler(FileTransferWatcher\_Created);
                      }
                      catch (Exception ex)
                      {
                          EventLog.WriteEntry("Message :  " + ex.Message);
                      }
                  }
              
                  void FileTransferWatcher\_Created(object sender, System.IO.FileSystemEventArgs e)
                  {
                      System.IO.File.Copy(e.FullPath, @"D:\\");
                  }
              
                  protected override void OnStop()
                  {
                  }
              }
              

              }

              Service1.Designer.cs

              namespace FileTransfer
              {
              partial class Service1
              {
              /// <summary>
              /// Required designer variable.
              /// </summary>
              private System.ComponentModel.IContainer components;

                  /// <summary>
                  /// Clean up any resources being used.
                  /// </summary>
                  /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
                  protected override void Dispose(bool disposing)
                  {
                      if (disposing && (components != null))
                      {
                          components.Dispose();
                      }
                      base.Dispose(disposing);
                  }
              
                  #region Component Designer generated code
              
                  /// <summary> 
                  /// Required method for Designer support - do not modify 
                  /// the contents of this method with the code editor.
                  /// </summary>
                  private void InitializeComponent()
                  {
                      this.FileTransferWatcher = new System.IO.FileSystemWatcher();
                      // 
                      // FileTransferWatcher
                      // 
                      this.FileTransferWatcher.EnableRaisingEvents = true;
                      **this.FileTransferWatcher.Path = "D:\\\\Test\\\\";**
                      // 
                      // Service1
                      // 
                      this.ServiceName = "FileTransfer";
              
                  }
              
                  #endregion
              
                  private System.IO.FileSystemWatcher FileTra
              
              0 Offline
              0 Offline
              0x3c0
              wrote on last edited by
              #6

              This is just a guess, but does the directory exist? I've looked at the property's code through Reflector, and that's the only thing that will throw the exception. By the way, your File.Copy will fail, because you haven't specified a source filename, just the parent directory.

              OSDev :)

              1 Reply Last reply
              0
              • S sri_3464 0

                Service1.cs

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Data;
                using System.Diagnostics;
                using System.Linq;
                using System.ServiceProcess;
                using System.Text;
                using System.Configuration;

                namespace FileTransfer
                {
                public partial class Service1 : ServiceBase
                {
                public Service1()
                {
                InitializeComponent();
                }

                    protected override void OnStart(string\[\] args)
                    {
                        try
                        {
                            FileTransferWatcher.Created += new System.IO.FileSystemEventHandler(FileTransferWatcher\_Created);
                        }
                        catch (Exception ex)
                        {
                            EventLog.WriteEntry("Message :  " + ex.Message);
                        }
                    }
                
                    void FileTransferWatcher\_Created(object sender, System.IO.FileSystemEventArgs e)
                    {
                        System.IO.File.Copy(e.FullPath, @"D:\\");
                    }
                
                    protected override void OnStop()
                    {
                    }
                }
                

                }

                Service1.Designer.cs

                namespace FileTransfer
                {
                partial class Service1
                {
                /// <summary>
                /// Required designer variable.
                /// </summary>
                private System.ComponentModel.IContainer components;

                    /// <summary>
                    /// Clean up any resources being used.
                    /// </summary>
                    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
                    protected override void Dispose(bool disposing)
                    {
                        if (disposing && (components != null))
                        {
                            components.Dispose();
                        }
                        base.Dispose(disposing);
                    }
                
                    #region Component Designer generated code
                
                    /// <summary> 
                    /// Required method for Designer support - do not modify 
                    /// the contents of this method with the code editor.
                    /// </summary>
                    private void InitializeComponent()
                    {
                        this.FileTransferWatcher = new System.IO.FileSystemWatcher();
                        // 
                        // FileTransferWatcher
                        // 
                        this.FileTransferWatcher.EnableRaisingEvents = true;
                        **this.FileTransferWatcher.Path = "D:\\\\Test\\\\";**
                        // 
                        // Service1
                        // 
                        this.ServiceName = "FileTransfer";
                
                    }
                
                    #endregion
                
                    private System.IO.FileSystemWatcher FileTra
                
                T Offline
                T Offline
                Thomas Krojer
                wrote on last edited by
                #7

                Sorry, I try to understand what you are doing:

                void FileTransferWatcher_Created(object sender, System.IO.FileSystemEventArgs e)
                {
                System.IO.File.Copy(e.FullPath, @"D:\");
                }
                /pre>

                Are you copying a file to your observed directory when you register the handler?

                1 Reply Last reply
                0
                • K Keith Barrow

                  You need to set this.FileTransferWatcher.Path = "D:\\Test\\"; before this FileTransferWatcher.EnableRaisingEvents = true; As the inital path of a FileSystemWatcher is an empty string. Also, as a side note, hardcoding the paths in magic strings isn't a good plan (though obviously the code posted is, by definition, unfinished). I'd also move out the FileWatcher/Transfer code into a separate class that deals with this and let the service just handle the service related stuff. The other thing is, if you can, I'd run this as a console app and then convert it into a service later as debugging services is trickier.

                  CCC solved so far: 2 (including a Hard One!) 37!?!! - Randall, Clerks

                  S Offline
                  S Offline
                  sri_3464 0
                  wrote on last edited by
                  #8

                  Thanks Keefb! I got the mistake i have done... Also as you said i should have started with as a console application.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups