문자열이 유효한 Windows 디렉터리 (폴더) 경로인지 확인
사용자가 입력 한 문자열이 폴더 경로를 나타내는 데 유효한지 확인하려고합니다. 유효하다는 것은 올바른 형식을 의미합니다.
내 응용 프로그램에서 폴더는 설치 대상을 나타냅니다. 폴더 경로 가 유효한 경우 폴더가 있는지 확인하고 그렇지 않은 경우 새로 만들고 싶습니다.
나는 현재 IO.Directory.Exists( String path )
. 사용자가 문자열을 올바르게 형식화하지 않는 경우를 제외하고는 이것이 잘 작동한다는 것을 알았습니다. 이 경우이 메서드는 폴더가 존재하지 않음을 나타내는 false를 반환합니다. 그러나 이것은 나중에 폴더를 만들 수 없기 때문에 문제입니다.
내 인터넷 검색에서 정규 표현식을 사용하여 형식이 올바른지 확인하라는 제안을 찾았습니다. 정규 표현식에 대한 경험이 없으며 이것이 실행 가능한 접근 방식인지 궁금합니다. 내가 찾은 내용은 다음과 같습니다.
Regex r = new Regex( @"^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$" );
return r.IsMatch( path );
와 함께 정규 표현식 테스트는 것 Directory.Exists()
, 나에게 줄 충분한 경로가 유효하고 있는지 여부를 여부를 확인하는 방법을? OS 및 기타 요인에 따라 다를 수 있지만 프로그램은 Windows 사용자만을 대상으로 합니다.
전화 Path.GetFullPath
; 경로가 유효하지 않으면 예외가 발생합니다.
(예 : 상대 경로를 허용하려면 Word
, 전화) Path.IsPathRooted
.
나는 실제로 SLaks에 동의하지 않습니다. 그 솔루션은 저에게 효과적이지 않았습니다. 예상대로 예외가 발생하지 않았습니다. 그러나이 코드는 나를 위해 일했습니다.
if(System.IO.Directory.Exists(path))
{
...
}
Path.GetFullPath는 아래 예외 만 제공합니다.
ArgumentException 경로가 길이가 0 인 문자열이거나 공백 만 포함하거나 GetInvalidPathChars에 정의 된 하나 이상의 잘못된 문자를 포함합니다. -또는-시스템이 절대 경로를 검색 할 수 없습니다.
SecurityException 호출자에게 필요한 권한이 없습니다.
ArgumentNullException 경로가 null입니다.
NotSupportedException 경로에 볼륨 식별자의 일부가 아닌 콜론 ( ":")이 포함되어 있습니다 (예 : "c : \").
PathTooLongException 지정된 경로, 파일 이름 또는 둘 다 시스템 정의 최대 길이를 초과합니다. 예를 들어 Windows 기반 플랫폼에서 경로는 248 자 미만이어야하고 파일 이름은 260 자 미만이어야합니다.
다른 방법은 다음을 사용하는 것입니다.
/// <summary>
/// Validate the Path. If path is relative append the path to the project directory by default.
/// </summary>
/// <param name="path">Path to validate</param>
/// <param name="RelativePath">Relative path</param>
/// <param name="Extension">If want to check for File Path</param>
/// <returns></returns>
private static bool ValidateDllPath(ref string path, string RelativePath = "", string Extension = "")
{
// Check if it contains any Invalid Characters.
if (path.IndexOfAny(Path.GetInvalidPathChars()) == -1)
{
try
{
// If path is relative take %IGXLROOT% as the base directory
if (!Path.IsPathRooted(path))
{
if (string.IsNullOrEmpty(RelativePath))
{
// Exceptions handled by Path.GetFullPath
// ArgumentException path is a zero-length string, contains only white space, or contains one or more of the invalid characters defined in GetInvalidPathChars. -or- The system could not retrieve the absolute path.
//
// SecurityException The caller does not have the required permissions.
//
// ArgumentNullException path is null.
//
// NotSupportedException path contains a colon (":") that is not part of a volume identifier (for example, "c:\").
// PathTooLongException The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
// RelativePath is not passed so we would take the project path
path = Path.GetFullPath(RelativePath);
}
else
{
// Make sure the path is relative to the RelativePath and not our project directory
path = Path.Combine(RelativePath, path);
}
}
// Exceptions from FileInfo Constructor:
// System.ArgumentNullException:
// fileName is null.
//
// System.Security.SecurityException:
// The caller does not have the required permission.
//
// System.ArgumentException:
// The file name is empty, contains only white spaces, or contains invalid characters.
//
// System.IO.PathTooLongException:
// The specified path, file name, or both exceed the system-defined maximum
// length. For example, on Windows-based platforms, paths must be less than
// 248 characters, and file names must be less than 260 characters.
//
// System.NotSupportedException:
// fileName contains a colon (:) in the middle of the string.
FileInfo fileInfo = new FileInfo(path);
// Exceptions using FileInfo.Length:
// System.IO.IOException:
// System.IO.FileSystemInfo.Refresh() cannot update the state of the file or
// directory.
//
// System.IO.FileNotFoundException:
// The file does not exist.-or- The Length property is called for a directory.
bool throwEx = fileInfo.Length == -1;
// Exceptions using FileInfo.IsReadOnly:
// System.UnauthorizedAccessException:
// Access to fileName is denied.
// The file described by the current System.IO.FileInfo object is read-only.-or-
// This operation is not supported on the current platform.-or- The caller does
// not have the required permission.
throwEx = fileInfo.IsReadOnly;
if (!string.IsNullOrEmpty(Extension))
{
// Validate the Extension of the file.
if (Path.GetExtension(path).Equals(Extension, StringComparison.InvariantCultureIgnoreCase))
{
// Trim the Library Path
path = path.Trim();
return true;
}
else
{
return false;
}
}
else
{
return true;
}
}
catch (ArgumentNullException)
{
// System.ArgumentNullException:
// fileName is null.
}
catch (System.Security.SecurityException)
{
// System.Security.SecurityException:
// The caller does not have the required permission.
}
catch (ArgumentException)
{
// System.ArgumentException:
// The file name is empty, contains only white spaces, or contains invalid characters.
}
catch (UnauthorizedAccessException)
{
// System.UnauthorizedAccessException:
// Access to fileName is denied.
}
catch (PathTooLongException)
{
// System.IO.PathTooLongException:
// The specified path, file name, or both exceed the system-defined maximum
// length. For example, on Windows-based platforms, paths must be less than
// 248 characters, and file names must be less than 260 characters.
}
catch (NotSupportedException)
{
// System.NotSupportedException:
// fileName contains a colon (:) in the middle of the string.
}
catch (FileNotFoundException)
{
// System.FileNotFoundException
// The exception that is thrown when an attempt to access a file that does not
// exist on disk fails.
}
catch (IOException)
{
// System.IO.IOException:
// An I/O error occurred while opening the file.
}
catch (Exception)
{
// Unknown Exception. Might be due to wrong case or nulll checks.
}
}
else
{
// Path contains invalid characters
}
return false;
}
다음은 @SLaks 의 답변 에서 권장 하는 Path.GetFullPath 사용을 활용하는 솔루션입니다 .
여기에 포함 된 코드 IsValidPath(string path)
에서 호출자는 예외 처리 에 대해 걱정할 필요 가 없도록 설계되었습니다 .
또한 절대 경로TryGetFullPath(...)
를 안전하게 얻으려고 할 때 호출하는 메서드가 자체적으로 장점이 있음을 알 수 있습니다.
/// <summary>
/// Gets a value that indicates whether <paramref name="path"/>
/// is a valid path.
/// </summary>
/// <returns>Returns <c>true</c> if <paramref name="path"/> is a
/// valid path; <c>false</c> otherwise. Also returns <c>false</c> if
/// the caller does not have the required permissions to access
/// <paramref name="path"/>.
/// </returns>
/// <seealso cref="Path.GetFullPath"/>
/// <seealso cref="TryGetFullPath"/>
public static bool IsValidPath(string path)
{
string result;
return TryGetFullPath(path, out result);
}
/// <summary>
/// Returns the absolute path for the specified path string. A return
/// value indicates whether the conversion succeeded.
/// </summary>
/// <param name="path">The file or directory for which to obtain absolute
/// path information.
/// </param>
/// <param name="result">When this method returns, contains the absolute
/// path representation of <paramref name="path"/>, if the conversion
/// succeeded, or <see cref="String.Empty"/> if the conversion failed.
/// The conversion fails if <paramref name="path"/> is null or
/// <see cref="String.Empty"/>, or is not of the correct format. This
/// parameter is passed uninitialized; any value originally supplied
/// in <paramref name="result"/> will be overwritten.
/// </param>
/// <returns><c>true</c> if <paramref name="path"/> was converted
/// to an absolute path successfully; otherwise, false.
/// </returns>
/// <seealso cref="Path.GetFullPath"/>
/// <seealso cref="IsValidPath"/>
public static bool TryGetFullPath(string path, out string result)
{
result = String.Empty;
if (String.IsNullOrWhiteSpace(path)) { return false; }
bool status = false;
try
{
result = Path.GetFullPath(path);
status = true;
}
catch (ArgumentException) { }
catch (SecurityException) { }
catch (NotSupportedException) { }
catch (PathTooLongException) { }
return status;
}
이 코드 사용
string DirectoryName = "Sample Name For Directory Or File";
Path.GetInvalidFileNameChars().Where(x => DirectoryName.Contains(x)).Count() > 0 || DirectoryName == "con"
private bool IsValidPath(string path)
{
Regex driveCheck = new Regex(@"^[a-zA-Z]:\\$");
if (!driveCheck.IsMatch(path.Substring(0, 3))) return false;
string strTheseAreInvalidFileNameChars = new string(Path.GetInvalidPathChars());
strTheseAreInvalidFileNameChars += @":/?*" + "\"";
Regex containsABadCharacter = new Regex("[" + Regex.Escape(strTheseAreInvalidFileNameChars) + "]");
if (containsABadCharacter.IsMatch(path.Substring(3, path.Length - 3)))
return false;
DirectoryInfo dir = new DirectoryInfo(Path.GetFullPath(path));
if (!dir.Exists)
dir.Create();
return true;
}
이 코드에 문제가 없었습니다.
private bool IsValidPath(string path, bool exactPath = true)
{
bool isValid = true;
try
{
string fullPath = Path.GetFullPath(path);
if (exactPath)
{
string root = Path.GetPathRoot(path);
isValid = string.IsNullOrEmpty(root.Trim(new char[] { '\\', '/' })) == false;
}
else
{
isValid = Path.IsPathRooted(path);
}
}
catch(Exception ex)
{
isValid = false;
}
return isValid;
}
예를 들어 다음은 false를 반환합니다.
IsValidPath("C:/abc*d");
IsValidPath("C:/abc?d");
IsValidPath("C:/abc\"d");
IsValidPath("C:/abc<d");
IsValidPath("C:/abc>d");
IsValidPath("C:/abc|d");
IsValidPath("C:/abc:d");
IsValidPath("");
IsValidPath("./abc");
IsValidPath("/abc");
IsValidPath("abc");
IsValidPath("abc", false);
그리고 이것은 true를 반환합니다.
IsValidPath(@"C:\\abc");
IsValidPath(@"F:\FILES\");
IsValidPath(@"C:\\abc.docx\\defg.docx");
IsValidPath(@"C:/abc/defg");
IsValidPath(@"C:\\\//\/\\/\\\/abc/\/\/\/\///\\\//\defg");
IsValidPath(@"C:/abc/def~`!@#$%^&()_-+={[}];',.g");
IsValidPath(@"C:\\\\\abc////////defg");
IsValidPath(@"/abc", false);
'your programing' 카테고리의 다른 글
이것에 대한 boost :: shared_ptr 얻기 (0) | 2020.10.16 |
---|---|
R에서 한 명령의 출력 억제 (0) | 2020.10.16 |
소프트 키보드의 ActionBar SearchView 자동 축소 닫기 (0) | 2020.10.16 |
HTML (0) | 2020.10.15 |
개체를 쓰는 동안 힘내가 멈춤 (0) | 2020.10.15 |