title: How to check access rights on remote hosts and local and UNC paths?
body: <p>I write a program, where the user can select local and remote directories. Before performing any operation on a selected path, I want to check if it's valid and if the user has read/write permission(s) granted on it. That's what I have:</p>
  
  <pre><code>private bool checkInput(string dirPath, string depthStr, string filename)
  {
      ...
      string reason = null;
      try
      {
          ...
          else if ((reason = CAN_ACCESS(dirPath)) == null)
          {
              if (!(new DirectoryInfo(dirPath)).Exists)
                  reason = string.Format("The directory '{0}' is no directory or does not exist!", dirPath);
              ...
          }
      }
      catch (Exception ex)
      {
          reason = ex.Message;
      }
  
      if (reason != null)
      {
          MessageBox.Show(reason, "Wrong input", MessageBoxButton.OK, MessageBoxImage.Error);
          return false;
      }
      return true;
  }
  
  public static string CAN_ACCESS(string dirPath)
  {
      Uri uri = new Uri(dirPath);
      //UserFileAccessRights rights = new UserFileAccessRights(dirPath);
  
      string errorMsg = null;
      if (uri.IsUnc &amp;&amp; !haveAccessPermissionOnHost(uri))
      {
          string domain = new Uri(dirPath).Host;
          domain = dirPath;
          ErrorClass res = PinvokeWindowsNetworking.connectToRemote(domain, null, null, true);
          if (res == null)
              return null;
          else
              errorMsg = string.Format("(Code {0}): {1}", res.num, res.message);
      }
  
      return errorMsg;
  }
  
  private static bool haveAccessPermissionOnHost(Uri uri)
  {
      throw new NotImplementedException();
  }
  </code></pre>
  
  <p><strong>The primary question</strong> is, how to simple check in <code>haveAccessPermissionOnHost(Uri uri),</code> if a user has read/write permission(s) on a remote host and unc path granted. </p>
  
  <p><strong>Hint:</strong>
  If no permissions granted, the well known Windows logon window pops up to force the user to enter some credentials, which is working fine:</p>
  
  <pre><code>ErrorClass res = PinvokeWindowsNetworking.connectToRemote(domain, null, null, true);
  </code></pre>
  
  <p>I got it from <a href="https://stackoverflow.com/questions/659013/accessing-a-shared-file-unc-from-a-remote-non-trusted-domain-with-credentials">this question</a> (and changed the <a href="http://lookfwd.doitforme.gr/blog/media/PinvokeWindowsNetworking.cs" rel="nofollow noreferrer">provided code</a> to return the <code>ErrorClass</code>, null on success).</p>
  
  <p><strong>My secondary question</strong> is, if my structure in general is the way to go to check a path, whether it's local or remote, for validity (that's why I provided more code as necessary).</p>