[工作问题总结]Directory.Exists 判断域中的共享文件夹

来源:互联网 发布:阿里云香港ss 编辑:程序博客网 时间:2024/04/29 11:00

 

------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------

 

工作中遇到一个问题,Directory.Exists 判断域中的共享文件夹,明明存在,却返回false

最终解决方案: 需要using 链接域中共享文件夹所在的服务器 删除文件的时候 也需要

重要代码如下:

/// <summary>/// 得到当前路劲下方的所有文件/// </summary>/// <param name="clientName"></param>/// <param name="type"></param>/// <param name="no"></param>/// <returns></returns>public static FileInfo[] TransShow(string clientName, string type, string no){var domain = string.Empty; //域var userName = string.Empty; //用户名var pwd = string.Empty; //密码var targetFilePath = string.Empty; //路径try{LinkDomain(type, ref domain, ref userName, ref pwd, ref targetFilePath); //连接域所用数据赋值targetFilePath += type == "2" || type == "3"? @"\" + no + @"\" + clientName + @"\" + DateTime.Now.Year + @"\": @"\" + clientName + @"\" + DateTime.Now.Year + @"\";using (new IdentityScope(domain, userName, pwd, LogonType.NewCredentials, LogonProvider.Default)){if (!Directory.Exists(targetFilePath)) //判断该路劲是否存在,不存在直接返回Nullreturn null;}}catch (Exception ex){Log.Error(" Exception: " + ex.Message);}var info = new DirectoryInfo(targetFilePath); //路劲存在的话,得到该路劲下方的所有文件var fileInfo = info.GetFiles();return fileInfo;}

 

/// <summary>/// 连接域所用数据赋值/// </summary>/// <param name="type">类型</param>/// <param name="domain">域</param>/// <param name="userName">用户名</param>/// <param name="pwd">密码</param>/// <param name="targetFilePath">路径</param>public static void LinkDomain(string type, ref string domain, ref string userName, ref string pwd, ref string targetFilePath){try{switch (type){case "2": //合约{domain = GlobalCaches.GetSystemConfig(false).ContractDomain;userName = GlobalCaches.GetSystemConfig(false).ContractUid;pwd = GlobalCaches.GetSystemConfig(false).ContractPwd;targetFilePath = GlobalCaches.GetSystemConfig(false).UploadPathContract;} break;case "3": //机器回厂{domain = GlobalCaches.GetSystemConfig(false).MachineDomain;userName = GlobalCaches.GetSystemConfig(false).MachineUid;pwd = GlobalCaches.GetSystemConfig(false).MachinePwd;targetFilePath = GlobalCaches.GetSystemConfig(false).UploadPathMachine;} break;case "4": //客户资料首批汇入{domain = GlobalCaches.GetSystemConfig(false).ClientDataFirstDomain;userName = GlobalCaches.GetSystemConfig(false).ClientDataFirstUid;pwd = GlobalCaches.GetSystemConfig(false).ClientDataFirstPwd;targetFilePath = GlobalCaches.GetSystemConfig(false).UploadPathClientDataFirst;} break;default: //客户{domain = GlobalCaches.GetSystemConfig(false).ClientDomain; //域userName = GlobalCaches.GetSystemConfig(false).ClientUid; //用户名pwd = GlobalCaches.GetSystemConfig(false).ClientPwd; //密码targetFilePath = GlobalCaches.GetSystemConfig(false).UploadPathClient;} break;}}catch (Exception ex){Log.Error(" Exception: " + ex.Message);}}

 

IdentityScope
public IdentityScope(string domain, string userName, string password, LogonType logonType, LogonProvider logonProvider){var logger = log4net.LogManager.GetLogger(GetType());try{if (string.IsNullOrEmpty(userName)){userName = "";}if (string.IsNullOrEmpty(domain)){domain = ".";}IntPtr token;var errorCode = 0;if (LogonUser(userName, domain, password, logonType, logonProvider, out token)){if (!ImpersonateLoggedOnUser(token)){errorCode = GetLastError();}}else{errorCode = GetLastError();}}catch (Exception ex){logger.Error(ex.Message);return;}}


private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword,LogonType dwLogonType, LogonProvider dwLogonProvider, out IntPtr phToken);


 

------------------------------ASP.Net+Android+IO开发 .Net培训 期待与您交流!------------------------------

 


原创粉丝点击