Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.IO functions not working in Windows Service

System.IO functions not working in Windows Service

Scheduled Pinned Locked Moved C#
questioncsharpsysadminxmlhelp
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MNFlyer
    wrote on last edited by
    #1

    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

    R M realJSOPR 3 Replies Last reply
    0
    • M MNFlyer

      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

      R Offline
      R Offline
      Robert Rohde
      wrote on last edited by
      #2

      Hi, my first hint would be that the local system account doesn't have the appropriate rights to access the network folder. Robert

      1 Reply Last reply
      0
      • M MNFlyer

        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

        M Offline
        M Offline
        MNFlyer
        wrote on last edited by
        #3

        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.

        P 1 Reply Last reply
        0
        • M MNFlyer

          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.

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          Or periodically check for the existance of the drive. P.S. I don't use mapped drives. (They're so '80s!)

          1 Reply Last reply
          0
          • M MNFlyer

            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

            realJSOPR Offline
            realJSOPR Offline
            realJSOP
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups