title: C# Directory.exist always return false on the local network
body: <p>I'm trying to check wether a directory exist on not or a local network. After some research on stackoverflow and MSDN, I develop my code by using impersonate method. The problem is it's not working very well, The <code>Directory.exists()</code> method always return <code>False</code> Here you have my code (it's nearly the same as the one from <a href="http://msdn.microsoft.com/en-us/library/w070t6ka.aspx?cs-save-lang=1&amp;cs-lang=csharp#code-snippet-2" rel="nofollow noreferrer">MSDN</a>):  </p>
  
  <pre><code>public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid
      {
          private SafeTokenHandle()
              : base(true)
          {
          }
  
          [DllImport("kernel32.dll")]
          [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
          [SuppressUnmanagedCodeSecurity]
          [return: MarshalAs(UnmanagedType.Bool)]
          private static extern bool CloseHandle(IntPtr handle);
  
          protected override bool ReleaseHandle()
          {
              return CloseHandle(handle);
          }
      }
  
   class Environment
      {
          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
          public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
              int dwLogonType, int dwLogonProvider, out SafeTokenHandle phToken);
  
          [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
          public extern static bool CloseHandle(IntPtr handle);
          const int LOGON32_PROVIDER_DEFAULT = 0;
          const int LOGON32_LOGON_INTERACTIVE = 2;
  
          private void m_SendAlertes()
          {
                  SafeTokenHandle safeTokenHandle;
                  string v_pathToDir = "\\192.168.1.199\Clients SiteInternet";
  
                  if (!LogonUser("RKalculateur", "SERVEUR2", 
                                  "riskedge", LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle))
                  {
                      int ret = Marshal.GetLastWin32Error();
                      throw new System.ComponentModel.Win32Exception(ret);
                  }
                  using (safeTokenHandle)
                  {
                      using (WindowsIdentity newId = new WindowsIdentity(safeTokenHandle.DangerousGetHandle()))
                      {
                          using (WindowsImpersonationContext impersonatedUser = newId.Impersonate())
                          {
                              if (Directory.Exists(@v_pathToDir))
                              {
                                 // Proceed code here
                              }
                          }
                      }
                  }
          }
      }
  </code></pre>
  
  <p>Here you have a picture of the rights for this directory :
  <img src="https://i.stack.imgur.com/3jYtX.png" alt="enter image description here"></p>