locked
"The path is not of a legal form." on trying to start the windows service RRS feed

  • Question

  • 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 referring to is the path that i set to the FileSystemWatcher.Path property. I don't understand why it happens. The path is "D:\Test". The folder has all the permission to access.

    Can anyone help me out.
    Thursday, December 31, 2009 11:17 AM

Answers

  • Hi,

    It seems you have got the answer here:
    http://www.codeproject.com/Messages/3318345/Re-The-path-is-not-of-a-legal-form-on-trying-to-st.aspx

    As keefb suggested:

    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.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Louis.fr Wednesday, January 6, 2010 12:49 PM
    • Marked as answer by Harry Zhu Thursday, January 7, 2010 6:15 AM
    Wednesday, January 6, 2010 3:35 AM

All replies

  • Can you post your code, please?
    Thursday, December 31, 2009 11:24 AM
  • 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 FileTransferWatcher;
    }
    }
    The statement below throws the exception on starting the service thats being installed.
     this.FileTransferWatcher.Path = "D:\\Test\\";
    
    Thursday, December 31, 2009 11:29 AM
  • I have also added an Installer class with components Servicecontroller, serviceinstaller and serviceprocessinstaller to the project. Is it making the problem?
    Thursday, December 31, 2009 11:39 AM
  • Hi,

    I have not test the project , but have you tried "D:\\Test";?

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, January 4, 2010 4:53 AM
  • Hi,

    It seems you have got the answer here:
    http://www.codeproject.com/Messages/3318345/Re-The-path-is-not-of-a-legal-form-on-trying-to-st.aspx

    As keefb suggested:

    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.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Proposed as answer by Louis.fr Wednesday, January 6, 2010 12:49 PM
    • Marked as answer by Harry Zhu Thursday, January 7, 2010 6:15 AM
    Wednesday, January 6, 2010 3:35 AM