检查字符串是否是有效的 Windows 目录(文件夹)路径
我试图确定用户输入的字符串对于表示文件夹路径是否有效。我所说的有效是指格式正确。
在我的应用程序中,该文件夹代表安装目标。假设文件夹路径有效,我想确定该文件夹是否存在,如果不存在则创建它。
我目前正在使用 IO.Directory.Exists( String path ) 。我发现这工作正常,除非用户没有正确格式化字符串。当发生这种情况时,此方法将返回 false,表示该文件夹不存在。但这是一个问题,因为之后我将无法创建该文件夹。
从我的谷歌搜索中,我发现了一个使用正则表达式来检查格式是否正确的建议。我没有使用正则表达式的经验,想知道这是否是一种可行的方法。这是我发现的:
Regex r = new Regex( @"^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$" );
return r.IsMatch( path );
将正则表达式测试与 Directory.Exists() 结合使用,给我一个足够好的方法来检查路径是否有效以及是否存在?我知道这会因操作系统和其他因素而异,但该程序仅针对 Windows 用户。
I am trying to determine whether a string input by a user is valid for representing a path to a folder. By valid, I mean formatted properly.
In my application, the folder represents an installation destination. Provided that the folder path is valid, I want to determine if the folder exists, and create it if it does not.
I am currently using IO.Directory.Exists( String path )
. I find that this works fine except when the user does not format the string properly. When that happens, this method will return false which indicates that the folder does not exist. But this is a problem because I won't be able to create the folder afterwards.
From my googling I found a suggestion to use a regular expression to check if the format is proper. I have no experience with regular expressions, and am wondering if that is a viable approach. Here's what I found:
Regex r = new Regex( @"^(([a-zA-Z]\:)|(\\))(\\{1}|((\\{1})[^\\]([^/:*?<>""|]*))+)$" );
return r.IsMatch( path );
Would a regular expression test in combination with Directory.Exists()
, give me a good enough method to check if the path is valid and whether it exists? I know this will vary with the OS and other factors, but the program is targeted for Windows users only.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
调用
Path.GetFullPath
;如果路径无效,它将抛出异常。要禁止相对路径(例如
Word
),请调用Path.IsPathRooted
。Call
Path.GetFullPath
; it will throw exceptions if the path is invalid.To disallow relative paths (such as
Word
), callPath.IsPathRooted
.我实际上不同意 SLAks。该解决方案对我不起作用。异常没有按预期发生。但这段代码对我有用:
I actually disagree with SLaks. That solution did not work for me. Exception did not happen as expected. But this code worked for me:
Path.GetFullPath 仅给出以下例外
另一种方法是使用以下方法:
Path.GetFullPath gives below exceptions only
Alternate way is to use the following :
这是一个利用 Path.GetFullPath 按照@SLaks的答案中的建议。
在我在这里包含的代码中,请注意
IsValidPath(string path)
的设计使得调用者不必担心异常处理。您可能还会发现,当您希望安全地尝试获取绝对路径时,它调用的方法
TryGetFullPath(...)
也有其自身的优点。Here is a solution that leverages the use of Path.GetFullPath as recommended in the answer by @SLaks.
In the code that I am including here, note that
IsValidPath(string path)
is designed such that the caller does not have to worry about exception handling.You may also find that the method that it calls,
TryGetFullPath(...)
, also has merit on its own when you wish to safely attempt to get an absolute path.使用此代码
Use this Code
一个更简单的独立于操作系统的解决方案:
继续尝试创建实际目录; 如果出现问题或名称无效,操作系统将自动抱怨并且代码将抛出。
用法:
Directory.CreateDirectory()
将自动抛出以下所有情况:A simpler OS-independent solution:
Go ahead and attempt to create the actual directory; if there is an issue or the name is invalid, the OS will automatically complain and the code will throw.
Usage:
Directory.CreateDirectory()
will automatically throw in all of the following situations:我对这段代码没有任何问题:
例如,这些将返回 false:
这些将返回 true:
I haven't had any problems with this code:
For example these would return false:
And these would return true: