Raja Bhushan

Hi All,

I am a beginner in this field... i need some help from u...

Can u please tell me how to do this ..... i just want how to execute the EXE from a .net service and in the EXE how to get the file that is recently copied into a particular location.

I would be greatly honoured if you could provide me with the details.

Thanks

Raja Bhushan



Re: Visual C# General Run a exe from .net service

Mattias Sjogren

Raja Bhushan wrote:

Can u please tell me how to do this ..... i just want how to execute the EXE from a .net service

System.Diagnostics.Process.Start will do that.

Raja Bhushan wrote:

and in the EXE how to get the file that is recently copied into a particular location.

What file And what do you mean by "getting" the file






Re: Visual C# General Run a exe from .net service

Raja Bhushan

Thanks Mattias !

It was a great help for me.

what i wanted to know is that how do we make a particular EXE when a service starts to run. If its by linking the exe to the service then how to link the exe and where to link that

What i mean from "getting" the file is, how to get the Name of the file that is recently copied into a particular predefined location

I have one more doubt. How do we make a service run at a particular time of the day





Re: Visual C# General Run a exe from .net service

Citizen on the earth

Hi Raja,

For your three questions, my understanding as follows(Suppose you run a windows service):

1) How to make a particular EXE when a service starts to run

As I understand, you can start a particular EXE when a service starts to run as follow:

Try to create a windows service project and start a particular EXE in the OnStart of the windows service.You can refer to the following sample codes:

Code Snippet
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
Process pEXE = new Process();
pEXE.StartInfo.FileName = @"c:\Test.EXE";
pEXE.StartInfo.UseShellExecute = true;
pEXE.Start();

}

2) How to get the Name of the file that is recently copied into a particular predefined location

I).If you have more than one file copied into a particular predefined location, you can try the following sample codes to get the file names:

Code Snippet
public string[] GetFileNames(string directory)
{
string[] files = Directory.GetFiles(directory);
if(files.Length !=0 || files != null)
{
string[] fileNames = new string[files.Length];
int index = -1;
for(int i= 0;i< files.Length;i++)
{
index = files[i].LastIndexOf("\\");
fileNames[i] = files[i].Substring(index+1);
}
return fileNames;
}
else return null;
}

II) If there is only one file copied a particular predefined location, you can try the the following sample codes to get the file name:

Code Snippet

public string GetFileName(string directory)
{
string[] files = Directory.GetFiles(directory);
if(files.Length !=0 || files != null)
{
string fileName = string.Empty;
int index = -1;
index = files[0].LastIndexOf("\\");
fileName = files[0].Substring(index+1);
return fileName;
}
else return null;
}

3) As far as I know, once you start a windows service, it will be running all the time until you stop it. If you would like to run a service at a specified time, you can try to use a timer and give a specified time to it. You can try to refer to the following article about "Using a Timer In A Windows Service" for details - http://csharpstruggles.blogspot.com/2005/02/using-timer-in-windows-service.html

Hope this helps,

Regards,

Citizens on the earth