title: Using WNetUseConnection to connect to a network drive
body: <p>I'm using WNetUseConnection to connect to a network drive, everything was working fine yesterday, below is the code I used to connect to the network drive.</p>
  <pre><code>NETRESOURCE nr = new NETRESOURCE();
  nr.dwType = RESOURCETYPE_DISK;
  nr.lpRemoteName = remote;
  nr.lpLocalName = &quot;L:&quot;;
  int ret = WNetUseConnection(IntPtr.Zero, nr, username, password, 0, null, null, null);
  </code></pre>
  <p>I find the value of remote using the below code</p>
  <pre><code>WNetGetConnection(&quot;L:&quot;, sb, ref size);          
  var remote= sb.ToString();
  </code></pre>
  <p>And I cancel the connection using the below code</p>
  <pre><code>WNetCancelConnection2(&quot;L:&quot;, 0, false); 
  </code></pre>
  <p>As I mentioned before everything worked fine yesterday, but today when I tried to retest the code it failed. The WNetGetConnection returned the error 1201 in some cases and 1202 in others (note that the drive was showing an x sign which means it was disconnected)</p>
  <p><a href="https://i.stack.imgur.com/6fYUL.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6fYUL.jpg" alt="enter image description here" /></a></p>
  <p>Trying now to reconnect to the drive using WNetUseConnection and the valid credentials failed with error 1219 which was really weird since there was no established connections to the drive, <strong>meanwhile trying to connect to the drive with passing empty username and password work just fine.</strong></p>
  <p>Using WNetCancelConnection2 to cancel the connection works and the connection is cancelled successfully but trying to execute WNetUseConnection again to retest the steps throws this error 67 which doesn't make sense since My PC is still showing the drive, but when I try to double click on the drive it shows me the error that the drive doesn't exist.</p>
  <p>Disconnect the drive won't work, but remapping the drive to the same location with the same drive letter worked and replaced the network drive. but remapping didn't solve the issue.</p>
  <p>When I deleted and recreated the drive using the below commands everything worked fine again.</p>
  <blockquote>
  <p>net use * /del /yes</p>
  <p>net use l: &quot;NetworkPath&quot; /user:&quot;username&quot; &quot;password&quot;</p>
  </blockquote>
  <p>Can anyone tells me why this happened? I appreciate the help.</p>
  <p>Edit:</p>
  <p>The problem started to show more often now, I cannot connect to the drive using the code, but the connection is successfully created when I double click on the drive icon on my computer. The commands are no longer solving the issue.</p>
  <p>PS: I'm using windows 10 version 1709</p>