/// <summary> /// Connects the unc path. /// </summary> /// <param name="path">The path.</param> /// <param name="credentials">The credentials.</param> /// <returns>System.Int32.</returns> private int ConnectUncPath(string path, NetworkCredential credentials) { CDFMonitor.LogOutputHandler("DEBUG:ConnectUncPath:enter:" + path); string userName = credentials.UserName; if (!string.IsNullOrEmpty(credentials.Domain)) { userName = string.Format("{0}\\{1}", credentials.Domain, credentials.UserName); } if (GetPathType(path) != ResourceType.Unc) { return(-1); } DeterminePathObjType objectType = DeterminePathObj(path); if (objectType == DeterminePathObjType.File) { path = Path.GetDirectoryName(FileManager.GetFullPath(path)); CDFMonitor.LogOutputHandler("DEBUG:ConnectUncPath:new path:" + path); } int ret = WindowsNetworking.ConnectToRemote(path, userName, credentials.Password, false); CDFMonitor.LogOutputHandler(string.Format("DEBUG:ConnectToRemote:return:{0}:{1}", path, ret == 0 ? true : false)); return(ret); }
/// <summary> /// Checks Unc Path existence /// </summary> /// <param name="path">The path.</param> /// <param name="nc">The nc.</param> /// <param name="create">if set to <c>true</c> [create].</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> private bool CheckUncPath(string path, NetworkCredential nc, bool create = false) { if (string.IsNullOrEmpty(path)) { return(false); } ResourceManagement.ResourceType rt = GetPathType(path); string originalPath = path.ToString(); if (rt == ResourceManagement.ResourceType.Unc) { // strip path to \\server\share string pattern = @"^\\\\[^\\]+?\\[^\\]+"; if (Regex.IsMatch(path, pattern)) { path = Regex.Matches(path, pattern)[0].Groups[0].Value; } } else { CDFMonitor.LogOutputHandler(string.Format("Error:CheckUncPath:invalid argument:{0}", path)); return(false); } try { int ret = 0; if ((ret = ConnectUncPath(originalPath, nc)) == 0) { return(true); } else { CDFMonitor.LogOutputHandler(string.Format("CheckUncPath:1:Warning no access to directory:{0}:{1}", originalPath, ret)); if (string.Compare(path, originalPath) == 0) { return(false); } } if ((ret = ConnectUncPath(path, nc)) != 0) { CDFMonitor.LogOutputHandler(string.Format("CheckUncPath:2:Warning no access to directory:{0}:{1}", path, ret)); return(false); } DeterminePathObjType objectType = DeterminePathObj(originalPath); switch (objectType) { case DeterminePathObjType.File: if (File.Exists(originalPath) | Directory.Exists(Path.GetDirectoryName(originalPath))) { CDFMonitor.LogOutputHandler("CheckUncPath:file exists."); return(true); } if (create) { // only create dir Directory.CreateDirectory(Path.GetDirectoryName(originalPath)); return(true); } break; case DeterminePathObjType.Directory: if (Directory.Exists(originalPath)) { CDFMonitor.LogOutputHandler("CheckUncPath:Directory exists."); return(true); } if (create) { // only create dir Directory.CreateDirectory(originalPath); return(true); } break; case DeterminePathObjType.WildCard: if (Directory.Exists(Path.GetDirectoryName(originalPath))) { CDFMonitor.LogOutputHandler("CheckUncPath:Wildcard Directory exists."); return(true); } break; default: CDFMonitor.LogOutputHandler(string.Format("CheckUncPath:error unknown object:{0}", objectType)); return(false); } return(true); } catch (Exception e) { CDFMonitor.LogOutputHandler("DEBUG:CheckUncPathShare:Exception:" + e.ToString()); return(false); } finally { DisconnectUncPath(path); DisconnectUncPath(originalPath); } }