title: I need to access a network path
body: <p>I have a windows service and tried to map it using the pinvoke. The code works fine in a console application, but it has no effect in a windows service. According to this:</p>
  
  <p><a href="https://stackoverflow.com/questions/281951/net-use-command-in-a-windows-service">&quot;net use&quot; command in a Windows Service</a></p>
  
  <p>I should drop the net use approach and instead give the appropriate privileges, so that my service can access the UNC path ( \server\share\file-path ). How do I set up these privileges? I assume it's basic Windows privileges that should be set somewhere. Any help is appreciated.</p>
  
  <pre><code>[DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
  internal static extern System.UInt32 NetUseAdd(string UncServerName, int Level, ref USE_INFO_2 Buf, out uint ParmError);
  
  [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  internal struct USE_INFO_2
  {
      internal LPWSTR ui2_local;
      internal LPWSTR ui2_remote;
      internal LPWSTR ui2_password;
      internal DWORD ui2_status;
      internal DWORD ui2_asg_type;
      internal DWORD ui2_refcount;
      internal DWORD ui2_usecount;
      internal LPWSTR ui2_username;
      internal LPWSTR ui2_domainname;
  }
  </code></pre>
  
  <p>and... </p>
  
  <pre><code>       static void Main()
          {
              USE_INFO_2 useInfo = new USE_INFO_2();
              useInfo.ui2_remote = @"\\xx.xx.xx.xx\E$"; // "\\xx.xx.xx.xx\E$"
              useInfo.ui2_password = "********";
              useInfo.ui2_asg_type = 0;    //disk drive
              useInfo.ui2_usecount = 1;
              useInfo.ui2_username = "Admin";
              useInfo.ui2_domainname = "rendering";
              uint paramErrorIndex;
              uint returnCode = NetUseAdd(String.Empty, 2, ref useInfo, out paramErrorIndex);
              if (returnCode != 0)
              {
                  throw new Win32Exception((int)returnCode);
              }
  ...
  </code></pre>