C#:在您拥有 DirectoryInfo 的目录中创建新的 FileInfo

发布于 2024-07-26 16:16:10 字数 353 浏览 1 评论 0原文

我只是想知道您是否有例如:

var dir = new DirectoryInfo(@"C:\Temp");

是否有比这更简单/更清晰的方法来将新文件添加到该目录?

var file = new FileInfo(Path.Combine(dir.FullName, "file.ext"));

我想我可能可以制作一个扩展方法或其他东西,但很好奇是否已经存在一些在这里看不到的东西......我的意思是 DirectoryInfo 确实有 GetFiles()<例如 /code> 方法。

I was just wondering when you have for example:

var dir = new DirectoryInfo(@"C:\Temp");

Is there an easier/clearer way to add a new file to that directory than this?

var file = new FileInfo(Path.Combine(dir.FullName, "file.ext"));

I'm thinking I can probably just make an extension method or something, but curious if something already exists that can't see here... I mean the DirectoryInfo does have GetFiles() method for example.

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

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

发布评论

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

评论(3

十六岁半 2024-08-02 16:16:10

你想做什么? 标题为“创建新文件”。 FileInfo 对象不是文件;而是文件。 它是一个保存有关文件信息的对象(可能存在也可能不存在)。 如果您确实想要创建该文件,有多种方法可以实现。 最简单的方法之一是:

File.WriteAllText(Path.Combine(dir.FullName, "file.ext"), "some text");

如果您想基于 FileInfo 对象创建文件,则可以使用以下方法:

var dir = new DirectoryInfo(@"C:\Temp");
var file = new FileInfo(Path.Combine(dir.FullName, "file.ext"));
if (!file.Exists) // you may not want to overwrite existing files
{
    using (Stream stream = file.OpenWrite())
    using (StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write("some text");
    }
}

作为旁注:它是 dir.FullName,而不是dir.FullPath

What is it that you want to do? The title says "Creating a new file". A FileInfo object is not a file; it's an object holding information about a file (that may or may not exist). If you actually want to create the file, there are a number of ways of doing so. One of the simplest ways would be this:

File.WriteAllText(Path.Combine(dir.FullName, "file.ext"), "some text");

If you want to create the file based on the FileInfo object instead, you can use the following approach:

var dir = new DirectoryInfo(@"C:\Temp");
var file = new FileInfo(Path.Combine(dir.FullName, "file.ext"));
if (!file.Exists) // you may not want to overwrite existing files
{
    using (Stream stream = file.OpenWrite())
    using (StreamWriter writer = new StreamWriter(stream))
    {
        writer.Write("some text");
    }
}

As a side note: it is dir.FullName, not dir.FullPath.

我们只是彼此的过ke 2024-08-02 16:16:10

你为什么不使用:

File.Create(@"C:\Temp\file.ext");

var dir = new DirectoryInfo(@"C:\Temp");
File.Create(dir.FullName + "\\file.ext");

Why don't you use:

File.Create(@"C:\Temp\file.ext");

or

var dir = new DirectoryInfo(@"C:\Temp");
File.Create(dir.FullName + "\\file.ext");
嗼ふ静 2024-08-02 16:16:10

虽然确实存在 Directorynfo.GetFiles() 方法,但它们仅返回磁盘上实际存在的文件。 Path.Combine 是关于假设的路径。

尝试这些扩展方法:

    public static FileInfo CombineWithFileName(this DirectoryInfo directoryInfo, string fileName)
    {
        return new FileInfo(Path.Combine(directoryInfo.Name, fileName));
    }

    public static DirectoryInfo CombineWithDirectoryName(this DirectoryInfo directoryInfo, string directoryName)
    {
        return new DirectoryInfo(Path.Combine(directoryInfo.Name, directoryName));
    }

While there does exist Directorynfo.GetFiles() methods, they only return files that actually exist on disk. Path.Combine is about hypothetical paths.

Try these extension methods:

    public static FileInfo CombineWithFileName(this DirectoryInfo directoryInfo, string fileName)
    {
        return new FileInfo(Path.Combine(directoryInfo.Name, fileName));
    }

    public static DirectoryInfo CombineWithDirectoryName(this DirectoryInfo directoryInfo, string directoryName)
    {
        return new DirectoryInfo(Path.Combine(directoryInfo.Name, directoryName));
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文