/// <summary> /// Checks the path. /// </summary> /// <param name="path">The path.</param> /// <param name="create">if set to <c>true</c> [create].</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns> public static bool CheckPath(string path, bool create = false) { CDFMonitor.LogOutputHandler(string.Format("DEBUG:CheckPath: enter:{0}:{1}", path, create)); try { if (string.IsNullOrEmpty(path)) { CDFMonitor.LogOutputHandler("CheckPath: empty path. returning false."); return(false); } path = FileManager.GetFullPath(path); ResourceManagement rm = new ResourceManagement(); ResourceManagement.ResourceType rt = rm.GetPathType(path); if (rm.CheckResourceCredentials(path) != ResourceManagement.CommandResults.Successful) { // 131022 if multiple dirs in path do not exist above will fail. if (!create | rm.DeterminePathObj(path) != ResourceManagement.DeterminePathObjType.Directory) { CDFMonitor.LogOutputHandler("CheckPath: checkresourcecredentials failed. returning false."); return(false); } } if (rt == ResourceManagement.ResourceType.Unc && rm.CheckUncPath(path, create)) { CDFMonitor.LogOutputHandler("CheckPath: able to access unc. returning true."); return(true); } if (rt == ResourceManagement.ResourceType.Url) { return(true); } ResourceManagement.DeterminePathObjType dt = rm.DeterminePathObj(path); if (dt == ResourceManagement.DeterminePathObjType.Directory) { if (Directory.Exists(path)) { return(true); } if (create) { Directory.CreateDirectory(path); // reset creds in case they failed on non-existent dir above if (rm.CheckResourceCredentials(path, true) != ResourceManagement.CommandResults.Successful) { return(false); } else { return(true); } } CDFMonitor.LogOutputHandler("DEBUG:CheckPath: directory doesnt exist and not configured to create. returning false:" + path); return(false); } if (dt == ResourceManagement.DeterminePathObjType.File) { if (File.Exists(path) | Directory.Exists(Path.GetDirectoryName(path))) { return(true); } if (create) { Directory.CreateDirectory(Path.GetDirectoryName(path)); return(true); } CDFMonitor.LogOutputHandler("CheckPath: file doesnt exist and directory cannot be created. returning false."); return(false); } CDFMonitor.LogOutputHandler(string.Format("CheckPath: unknown object:{0}", dt)); return(false); } catch (Exception e) { CDFMonitor.LogOutputHandler("CheckPath: exception:" + e.ToString()); return(false); } }
/// <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); } }