获取不存在的文件夹名称的最佳方法是什么?

发布于 2024-07-06 12:10:24 字数 190 浏览 5 评论 0原文

获取包含我可以确定存在的文件夹名称的字符串的最佳方法是什么? 也就是说,如果我为给定路径调用DirectoryInfo.Exists,它应该返回 false。

编辑:其背后的原因是我正在为错误检查器编写测试,错误检查器测试路径是否存在,所以我大声想知道获取不存在的路径的最佳方法。

What's the best way to get a string containing a folder name that I can be certain does not exist? That is, if I call DirectoryInfo.Exists for the given path, it should return false.

EDIT: The reason behind it is I am writing a test for an error checker, the error checker tests whether the path exists, so I wondered aloud on the best way to get a path that doesn't exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

流心雨 2024-07-13 12:10:24

以 GUID 命名 - 只需去掉非法字符。

Name it after a GUID - just take out the illegal characters.

薔薇婲 2024-07-13 12:10:24

实际上没有任何方法可以精确地按照您想要的方式去做。 如果您考虑一下,您会发现即使在调用 DirectoryInfo.Exists 返回 false 之后,其他一些程序也可能会继续创建目录 - 这是一个竞争条件。

处理这个问题的正常方法是创建一个新的临时目录 - 如果创建成功,那么您就知道它在创建之前不存在。

There isn't really any way to do precisely what you way you want to do. If you think about it, you see that even after the call to DirectoryInfo.Exists has returned false, some other program could have gone ahead and created the directory - this is a race condition.

The normal method to handle this is to create a new temporary directory - if the creation succeeds, then you know it hadn't existed before you created it.

Spring初心 2024-07-13 12:10:24

好吧,在不创建目录的情况下,您所能确定的是它在几微秒前并不存在。 这是一种可能足够接近的方法:

        string path = Path.GetTempFileName();
        File.Delete(path);
        Directory.CreateDirectory(path);

请注意,这将创建目录以阻止线程竞争(即另一个进程/线程从您下面窃取目录)。

Well, without creating the directory, all you can be sure of is that it didn't exist a few microseconds ago. Here's one approach that might be close enough:

        string path = Path.GetTempFileName();
        File.Delete(path);
        Directory.CreateDirectory(path);

Note that this creates the directory to block against thread races (i.e. another process/thread stealing the directory from under you).

只是偏爱你 2024-07-13 12:10:24

我最终使用的是:(

using System.IO;

string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

另外,您似乎不需要从 guid 中删除字符 - 它们会生成合法的文件名)

What I ended up using:

using System.IO;

string path = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

(Also, it doesn't seem like you need to strip out chars from a guid - they generate legal filenames)

晨曦慕雪 2024-07-13 12:10:24

嗯,一个好的选择是将用户名、今天的日期和时间等字符串连接到毫秒。

但我很好奇:你为什么要这样做? 它应该用来做什么?

Well, one good bet will be to concatenate strings like the user name, today's date, and time down to the millisecond.

I'm curious though: Why would you want to do this? What should it be for?

如日中天 2024-07-13 12:10:24

这是要创建一个临时文件夹还是什么? 我经常使用 Guid.NewGuid 来获取一个字符串,用于表示您要确保该文件夹名称尚不存在的文件夹名称。

Is this to create a temporary folder or something? I often use Guid.NewGuid to get a string to use for the folder name that you want to be sure does not already exist.

如梦初醒的夏天 2024-07-13 12:10:24

我认为你可以足够接近:

string directoryName = Guid.NewGuid.ToSrtring();

由于 Guid 是相当随机的,所以你不应该得到 2 个相同的目录。

I think you can be close enough:

string directoryName = Guid.NewGuid.ToSrtring();

Since Guid's are fairly random you should not get 2 dirs the same.

も星光 2024-07-13 12:10:24

在命名空间中使用新生成的 GUID(例如,应用程序/产品的名称)应该可以满足您的需求。 例如,以下代码不可能失败:

string ParentPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MyAppName");
string UniquePath = System.IO.Path.Combine(ParentPath, Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(UniquePath);

Using a freshly generated GUID within a namespace that is also somewhat unique (for example, the name of your application/product) should get you what you want. For example, the following code is extremely unlikely to fail:

string ParentPath = System.IO.Path.Combine(Environment.GetEnvironmentVariable("TEMP"), "MyAppName");
string UniquePath = System.IO.Path.Combine(ParentPath, Guid.NewGuid().ToString());
System.IO.Directory.CreateDirectory(UniquePath);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文