"The path is not of a legal form." on trying to start the service created in c#
-
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.
-
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.
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.
-
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.
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
-
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
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
-
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
You need to set
this.FileTransferWatcher.Path = "D:\\Test\\";
before thisFileTransferWatcher.EnableRaisingEvents = true;
As the inital path of aFileSystemWatcher
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
-
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
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 :)
-
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
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?
-
You need to set
this.FileTransferWatcher.Path = "D:\\Test\\";
before thisFileTransferWatcher.EnableRaisingEvents = true;
As the inital path of aFileSystemWatcher
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
Thanks Keefb! I got the mistake i have done... Also as you said i should have started with as a console application.