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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Accessing a mapped drive in windows service

Accessing a mapped drive in windows service

Scheduled Pinned Locked Moved C#
csharpasp-netsecurityhelp
6 Posts 3 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
    mark_me
    wrote on last edited by
    #1

    Hi, i have a windows service that needs to read files from mapped drive. The drive is located at \\networkshare\mynetwork. This path has a a folder named ConfirmedOrders. so i am trying to read as: "file.readalllines(\\networkshare\mynetwork\confirmedorders\myfile.txt)" in windows service and i get an exception UnauthorizedAccess with msg "Access to path ... was denied". I have another asp.net page which connects to same location and is able to read the file. I have no idea why my service is not reading the file. The account that i am using for service is LocalSystem and yeah, the mapped drive has permissions for all users. I tried switching the service to user account but that requires username and password and am not sure on wat the username and password should be. Tried assingning a username and password and i get a msg that "no mapping between account names and security Ids was done". Any help would be greatly appreciated. Thanx

    P D 2 Replies Last reply
    0
    • M mark_me

      Hi, i have a windows service that needs to read files from mapped drive. The drive is located at \\networkshare\mynetwork. This path has a a folder named ConfirmedOrders. so i am trying to read as: "file.readalllines(\\networkshare\mynetwork\confirmedorders\myfile.txt)" in windows service and i get an exception UnauthorizedAccess with msg "Access to path ... was denied". I have another asp.net page which connects to same location and is able to read the file. I have no idea why my service is not reading the file. The account that i am using for service is LocalSystem and yeah, the mapped drive has permissions for all users. I tried switching the service to user account but that requires username and password and am not sure on wat the username and password should be. Tried assingning a username and password and i get a msg that "no mapping between account names and security Ids was done". Any help would be greatly appreciated. Thanx

      P Offline
      P Offline
      Paladin2000
      wrote on last edited by
      #2

      I recently created a simple service that moves files, but it may be similar enough to be useful. It uses a P/Invoke of mpr.dll to create a share without a drive letter; I did this because I ran into the same access denied errors that you mention above. Don't know if this will help or not, but it worked for what I was doing. Good luck!

      private const int RESOURCETYPE_DISK = 0x1;

      [DllImport("mpr.dll")]
      private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);

      [StructLayout(LayoutKind.Sequential)]
      private struct structNetResource
      {
      public int Scope;
      public int Type;
      public int DisplayType;
      public int Usage;
      public string LocalName;
      public string RemoteName;
      public string Comment;
      public string Provider;
      }

      /// <summary>
      /// Transfers the specified file to the pre-defined Destination folder.
      /// </summary>
      /// <param name="AbsoluteFilename">The full path of the file to transfer.</param>
      /// <param name="FileName">The name of the file, without the path.</param>
      private void fileTransfer(string AbsoluteFilename, string FileName)
      {
      try
      {
      //Define the directory mapping
      structNetResource stNetRes = new structNetResource();
      stNetRes.Scope = 2;
      stNetRes.Type = RESOURCETYPE_DISK;
      stNetRes.DisplayType = 3;
      stNetRes.Usage = 1;
      stNetRes.RemoteName = destinationFolder; //Path to destination folder
      stNetRes.LocalName = string.Empty; //This is the drive letter and colon (e.g., Q: )

          //Open the destination connection
          int i = WNetAddConnection2A(ref stNetRes, destinationPass, destinationUser, 0);
          if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
      
          //Move the file
          //  \*\*\* This is the line you would want to change \*\*\*
          if (File.Exists(AbsoluteFilename)) File.Move(AbsoluteFilename, destinationFolder + "\\\\" + FileName);
      }
      catch (Exception ex)
      {
          throw new System.Exception(ex.Message);
      }
      

      }

      modified on Monday, July 13, 2009 4:37 PM

      M 1 Reply Last reply
      0
      • P Paladin2000

        I recently created a simple service that moves files, but it may be similar enough to be useful. It uses a P/Invoke of mpr.dll to create a share without a drive letter; I did this because I ran into the same access denied errors that you mention above. Don't know if this will help or not, but it worked for what I was doing. Good luck!

        private const int RESOURCETYPE_DISK = 0x1;

        [DllImport("mpr.dll")]
        private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);

        [StructLayout(LayoutKind.Sequential)]
        private struct structNetResource
        {
        public int Scope;
        public int Type;
        public int DisplayType;
        public int Usage;
        public string LocalName;
        public string RemoteName;
        public string Comment;
        public string Provider;
        }

        /// <summary>
        /// Transfers the specified file to the pre-defined Destination folder.
        /// </summary>
        /// <param name="AbsoluteFilename">The full path of the file to transfer.</param>
        /// <param name="FileName">The name of the file, without the path.</param>
        private void fileTransfer(string AbsoluteFilename, string FileName)
        {
        try
        {
        //Define the directory mapping
        structNetResource stNetRes = new structNetResource();
        stNetRes.Scope = 2;
        stNetRes.Type = RESOURCETYPE_DISK;
        stNetRes.DisplayType = 3;
        stNetRes.Usage = 1;
        stNetRes.RemoteName = destinationFolder; //Path to destination folder
        stNetRes.LocalName = string.Empty; //This is the drive letter and colon (e.g., Q: )

            //Open the destination connection
            int i = WNetAddConnection2A(ref stNetRes, destinationPass, destinationUser, 0);
            if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
        
            //Move the file
            //  \*\*\* This is the line you would want to change \*\*\*
            if (File.Exists(AbsoluteFilename)) File.Move(AbsoluteFilename, destinationFolder + "\\\\" + FileName);
        }
        catch (Exception ex)
        {
            throw new System.Exception(ex.Message);
        }
        

        }

        modified on Monday, July 13, 2009 4:37 PM

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

        Hi, Thankyou and wow!!! First look through your code was confusing but ful of hope. I might need help though. Do i have to download mpr.dll? and also RESOURCETYPE_DISK (0X1)? also wat will be destinationPass,destinationUser. I thought UNC makes things easy and though my drive letter was Y: , i was using \\xyx\\abc. Thankyou again

        P 1 Reply Last reply
        0
        • M mark_me

          Hi, i have a windows service that needs to read files from mapped drive. The drive is located at \\networkshare\mynetwork. This path has a a folder named ConfirmedOrders. so i am trying to read as: "file.readalllines(\\networkshare\mynetwork\confirmedorders\myfile.txt)" in windows service and i get an exception UnauthorizedAccess with msg "Access to path ... was denied". I have another asp.net page which connects to same location and is able to read the file. I have no idea why my service is not reading the file. The account that i am using for service is LocalSystem and yeah, the mapped drive has permissions for all users. I tried switching the service to user account but that requires username and password and am not sure on wat the username and password should be. Tried assingning a username and password and i get a msg that "no mapping between account names and security Ids was done". Any help would be greatly appreciated. Thanx

          D Offline
          D Offline
          darkelv
          wrote on last edited by
          #4

          Could it be the user that is being set to run the service has no access to that folder?

          M 1 Reply Last reply
          0
          • D darkelv

            Could it be the user that is being set to run the service has no access to that folder?

            M Offline
            M Offline
            mark_me
            wrote on last edited by
            #5

            Yeah, it was because of the user. I figured it out though. Thanx for replying.

            1 Reply Last reply
            0
            • M mark_me

              Hi, Thankyou and wow!!! First look through your code was confusing but ful of hope. I might need help though. Do i have to download mpr.dll? and also RESOURCETYPE_DISK (0X1)? also wat will be destinationPass,destinationUser. I thought UNC makes things easy and though my drive letter was Y: , i was using \\xyx\\abc. Thankyou again

              P Offline
              P Offline
              Paladin2000
              wrote on last edited by
              #6

              Also, note that you may need to involve these namespaces: using System.IO; using Shell32; using System.Runtime.InteropServices; mpr.dll should be located in your Windows directory at the following location: C:\WINDOWS\system32. It's part of the OS. (I don't think that they changed it in Vista; I use XP.) RESOURCETYPE_DISK is a constant hex value; you can use that line as-is. destinationUser = "Domain\Username", e.g. @"MSHOME\Jane.Doe" or "MSHOME\\Jane.Doe" destinationPass = The password for the user above. destinationFolder = The path to the remote folder the user account has rights to. e.g., @"\\SERVER\ShareFolder" or "\\\\SERVER\\ShareFolder" Note that the purpose of the code snippet given was to move a file from the local machine to a remote share. You would need to modify it slightly to open files instead. (Change the last line, right before the Catch block.) You don't need to provide a drive letter to use this code; I leave mine as string.Empty.

              modified on Wednesday, July 15, 2009 12:16 PM

              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