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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1078116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:41:40  来源:igfitidea点击:

C#: Creating a new FileInfo in a directory that you have the DirectoryInfo of

c#filedirectory

提问by Svish

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 DirectoryInfodoes have GetFiles()method for example.

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

采纳答案by Fredrik M?rk

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 createthe file, there are a number of ways of doing so. One of the simplest ways would be this:

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

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

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

如果要改为基于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");
    }
}

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

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

回答by Bhaskar

Why don't you use:

你为什么不使用:

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

or

或者

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

回答by Jay Bazuzi

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

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

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));
    }