I\'m accessing other server with the login credentials. My problem is if I run the code initially, it wil show the error as
Found out the solution. See the updated code for the answer.
try
{
IntPtr admin_token = default(IntPtr);
//Added these 3 lines
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin = null;
WindowsImpersonationContext wic = null;
if(LogonUser("myusername","domain","pwd",9,0,ref admin_token) != 0)
{
//Newly added lines
wid_admin = new WindowsIdentity(admin_token);
wic = wid_admin.Impersonate();
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
}
}