System.IO functions not working in Windows Service
-
I created a C# Windows Service to clean up files on some equipment PCs. Along with the service I created a configuration Windows App. that allows me to install, start, stop, uninstall the service and create an configuration XML file to change folder locations and such. Purpose: The service uses an OnTimer event to periodically check a folder for files. It then creates a backup copy on a network drive and deletes the local file. Problem: The following code does not work in the service but will work if performed from a button in the configuration app. The AddToFile function is working in the service as it just writes the string to a text file. The Service is configured to log on as a local system. Question: Why does this not work from within the service? //Code Section AddToFile(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " Copying Reports to Network"); foreach (string sfi in Directory.GetFiles(SC.LocalReportFolder)) { FileInfo fi = new FileInfo(sfi); FileInfo fn; if (SC.CU.AppendMachineName) //Configuration file variable { //Adds the machine name to the report string nwname = fi.Name.Replace(fi.Extension, "_" + SC.MachineName + ".txt"); fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + nwname, true); } else { fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + SC.MachineName + "\\" + fi.Name, true); } fn.Refresh(); //If the copy was successful then delete the local file. if ((fn.Exists)&&(SC.CU.DeleteLocalReport)) { fi.Delete(); } } //End of Code
-
I created a C# Windows Service to clean up files on some equipment PCs. Along with the service I created a configuration Windows App. that allows me to install, start, stop, uninstall the service and create an configuration XML file to change folder locations and such. Purpose: The service uses an OnTimer event to periodically check a folder for files. It then creates a backup copy on a network drive and deletes the local file. Problem: The following code does not work in the service but will work if performed from a button in the configuration app. The AddToFile function is working in the service as it just writes the string to a text file. The Service is configured to log on as a local system. Question: Why does this not work from within the service? //Code Section AddToFile(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " Copying Reports to Network"); foreach (string sfi in Directory.GetFiles(SC.LocalReportFolder)) { FileInfo fi = new FileInfo(sfi); FileInfo fn; if (SC.CU.AppendMachineName) //Configuration file variable { //Adds the machine name to the report string nwname = fi.Name.Replace(fi.Extension, "_" + SC.MachineName + ".txt"); fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + nwname, true); } else { fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + SC.MachineName + "\\" + fi.Name, true); } fn.Refresh(); //If the copy was successful then delete the local file. if ((fn.Exists)&&(SC.CU.DeleteLocalReport)) { fi.Delete(); } } //End of Code
Hi, my first hint would be that the local system account doesn't have the appropriate rights to access the network folder. Robert
-
I created a C# Windows Service to clean up files on some equipment PCs. Along with the service I created a configuration Windows App. that allows me to install, start, stop, uninstall the service and create an configuration XML file to change folder locations and such. Purpose: The service uses an OnTimer event to periodically check a folder for files. It then creates a backup copy on a network drive and deletes the local file. Problem: The following code does not work in the service but will work if performed from a button in the configuration app. The AddToFile function is working in the service as it just writes the string to a text file. The Service is configured to log on as a local system. Question: Why does this not work from within the service? //Code Section AddToFile(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " Copying Reports to Network"); foreach (string sfi in Directory.GetFiles(SC.LocalReportFolder)) { FileInfo fi = new FileInfo(sfi); FileInfo fn; if (SC.CU.AppendMachineName) //Configuration file variable { //Adds the machine name to the report string nwname = fi.Name.Replace(fi.Extension, "_" + SC.MachineName + ".txt"); fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + nwname, true); } else { fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + SC.MachineName + "\\" + fi.Name, true); } fn.Refresh(); //If the copy was successful then delete the local file. if ((fn.Exists)&&(SC.CU.DeleteLocalReport)) { fi.Delete(); } } //End of Code
After some further searching I found that a service cannot use an existing mapped drive. It must create the map itself to work. This also has problems as a service may start prior log on an thus the network is not available. Looks like I may have to use my Windows App in the startup folder with a timer or filewatcher event.
-
After some further searching I found that a service cannot use an existing mapped drive. It must create the map itself to work. This also has problems as a service may start prior log on an thus the network is not available. Looks like I may have to use my Windows App in the startup folder with a timer or filewatcher event.
Or periodically check for the existance of the drive. P.S. I don't use mapped drives. (They're so '80s!)
-
I created a C# Windows Service to clean up files on some equipment PCs. Along with the service I created a configuration Windows App. that allows me to install, start, stop, uninstall the service and create an configuration XML file to change folder locations and such. Purpose: The service uses an OnTimer event to periodically check a folder for files. It then creates a backup copy on a network drive and deletes the local file. Problem: The following code does not work in the service but will work if performed from a button in the configuration app. The AddToFile function is working in the service as it just writes the string to a text file. The Service is configured to log on as a local system. Question: Why does this not work from within the service? //Code Section AddToFile(DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToLongTimeString() + " Copying Reports to Network"); foreach (string sfi in Directory.GetFiles(SC.LocalReportFolder)) { FileInfo fi = new FileInfo(sfi); FileInfo fn; if (SC.CU.AppendMachineName) //Configuration file variable { //Adds the machine name to the report string nwname = fi.Name.Replace(fi.Extension, "_" + SC.MachineName + ".txt"); fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + nwname, true); } else { fn = fi.CopyTo(SC.CU.NetworkReportFolder + "\\" + SC.MachineName + "\\" + fi.Name, true); } fn.Refresh(); //If the copy was successful then delete the local file. if ((fn.Exists)&&(SC.CU.DeleteLocalReport)) { fi.Delete(); } } //End of Code
If you're going to access a file system on another computer, your service has to be installed with a user account that exists on the remote system. The default user for a service is "Local System" or something like that. So, you need to have (or create a new) user account with appropriate permissions on the remote system, and then install your service with those account credentials on your local box. Put a try/catch block around your System.IO calls, and you'll see exactly why they're failing.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001