Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a windows service that activates at certain times of the day, to copy one file in a folder to another folder.
the filepaths:
Source: C:\\users\\bebe2011\\desktop\\excel
Destination: C:\\users\\bebe2011\\desktop\\newexcel
My code;
C#
protected override void OnStart(string[] args)
 {
     int hour = DateTime.Now.Hour;
     int minute = DateTime.Now.Minute;
     if (hour == 11 && minute == 40)
     {
         Copy_Excelfile_And_Paste_at_anotherloaction();
     }
 }

 protected override void OnStop()
 {
     Create_ServiceStoptextfile();
 }

 public static void Copy_Excelfile_And_Paste_at_anotherloaction()
 {
     try
     {
         string source = "C:\\users\\bebe2011\\desktop\\excels";
         string Destination = "C:\\users\\bebe2011\\desktop\\newexcel";
         string filename = string.Empty;
         if (!(Directory.Exists(Destination) && Directory.Exists(source)))
             return;
         string[] Templateexcelfile = Directory.GetFiles(source);
         foreach (string file in Templateexcelfile)
         {
             if (Templateexcelfile[0].Contains("Template"))
             {
                 filename = System.IO.Path.GetFileName(file);
                 Destination = System.IO.Path.Combine(Destination, filename.Replace(".xlsx", DateTime.Now.ToString("yyyyMMdd")) + ".xlsx");
                 System.IO.File.Copy(file, Destination, true);
             }
         }

     }
     catch (Exception ex)
     {
         Create_ErrorFile(ex);
     }

 }


What I have tried:

Im not quite sure what to do , because the ServiceStopTextFile is created in same folders with the status of the windows service?
Posted
Updated 10-Jun-16 6:52am
Comments
F-ES Sitecore 10-Jun-16 7:24am    
The Users folder is restricted so that only the owner account can access it. You'll need to run your service as an account that has access to the folder.
BEBE2011 10-Jun-16 7:30am    
the service is run as Local System
BEBE2011 10-Jun-16 7:39am    
The service is able to create a txt file -ServiceStopTextFile in the same folder?
ZurdoDev 10-Jun-16 8:23am    
The error will tell you everything. What is it?
BEBE2011 10-Jun-16 8:56am    
There is no error , the excel files just wont appear in the destination folder.

Try to debug your application, I know it is difficult with service. But it is possible.

Put this code into your main method
#if DEBUG
service.DebugMode();
Thread.Sleep(Timeout.Infinite);
#else
           
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
    service
};
ServiceBase.Run(ServicesToRun);       
#endif


and in your service you going to have DebugMode method:

public void DebugMode()
{
     this.OnStart(null);
}


and you can easily debug your service in VisualStudio in DEBUG mode.

here is a working example:
GitHub - xszaboj/debugWindowsService: simple solution how to debug windows service[^]
 
Share this answer
 
v2
Comments
BEBE2011 13-Jun-16 5:26am    
Hi , thats for the possible solution, its coming back with this error?
"An object reference is required for the non-static field, method, or property "
xszaboj 13-Jun-16 5:54am    
Hi, ok and on what line? Or better what piece of code?
BEBE2011 13-Jun-16 6:31am    
Hi, when I declare DebugMode() in service.
xszaboj 13-Jun-16 7:33am    
Hi, you can download simple demo from github that I setup ;).
Check Program.cs and Service1.cs
I suspect the line:
C#
if (Templateexcelfile[0].Contains("Template"))

should be:
C#
if (file.Contains("Template"))


Otherwise, you'll only copying the files if the first file in the source folder contains the word "Template".
 
Share this answer
 
Comments
BEBE2011 13-Jun-16 5:27am    
Hi, Thanks for the possible solution, i have change the code to what you have recommended.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900