networking - C# Directory.exist always return false on the local network -


i'm trying check wether directory exist on not or local network. after research on stackoverflow , msdn, develop code using impersonate method. problem it's not working well, directory.exists() method return false here have code (it's same 1 msdn):

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                             }                         }                     }                 }         }     } 

here have picture of rights directory : enter image description here

it's issue connected user permissions.

from msdn:

if not have @ minimum read-only permission directory, exists method return false.

if you're using local account , not domain account, using directory.exists() method problematic.

i had similar problem in past: had check if net share existed in network , there no domain. way didn't work me. in end, gave on directory.exists() method , ended using net use command ( http://www.cezeo.com/tips-and-tricks/net-use-command/ )

bool exists = false; string output = ""; string error = "";  system.diagnostics.process process = new system.diagnostics.process(); process = new system.diagnostics.process();             executeshellcommand(process, "net use", "\""+ @path + "\" "+                this.password+ " /user:"+machinename+"\\"+username + " /persistent:no",                ref output, ref error); console.writeline("\r\n\t__________________________"+                 "\r\n\toutput:" + output.trim().replace("\r", " ") +                 "\r\n\terror: " + error.trim().replace("\r"," "));              if (output.length>0 && error.length==0)             {                 exists = true;             }              process = new system.diagnostics.process();             executeshellcommand(process, "net use", " /delete " + @path,                 ref output, ref error); 

....

public void executeshellcommand(system.diagnostics.process process, string filetoexecute,         string command, ref string output, ref string error)     {         try         {             string cmd = string.format(system.globalization.cultureinfo.invariantculture, @"{0}\cmd.exe", new object[] { environment.systemdirectory });             string args = string.format(system.globalization.cultureinfo.invariantculture, "/c {0}", new object[] { filetoexecute });             if (command != null && command.length > 0)             {                 args += string.format(system.globalization.cultureinfo.invariantculture, " {0}", new object[] { command, system.globalization.cultureinfo.invariantculture });             }              system.diagnostics.processstartinfo startinfo = new system.diagnostics.processstartinfo(cmd, args);              startinfo.createnowindow = true;             startinfo.useshellexecute = false;             startinfo.redirectstandardoutput = true;             startinfo.redirectstandardinput = true;             startinfo.redirectstandarderror = true;              process.startinfo = startinfo;              process.start();              // timeout process.waitforexit(10 * 1000); output = process.standardoutput.readtoend();              error = process.standarderror.readtoend();         }         catch (win32exception e32)         {             console.writeline("win32 exception caught in process: {0}", e32.tostring());         }         catch (exception e         {             console.writeline("exception caught in process: {0}", e.tostring());         }                 {             // close process , cleanup             process.close();             process.dispose();             process = null;         }     } 

i know it's hack worked me , it's possibility. (although may need set proper net share)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -