在C# Cake中如何将文本文件读入List

huangapple go评论68阅读模式
英文:

In C# Cake How to Read a Text File into List<string>?

问题

在Cake构建系统中,如何将一个以回车分隔的文本文件(比如文件夹名称)读取到List<String>中呢?

当我尝试使用File类时,它抱怨File(string)是一个方法,不适用于给定的上下文:

filePath filePath;
string fileContents = File.ReadAllText(filePath FullPath);

我该如何编写代码,使其在Cake中正常工作?

https://cakebuild.net/

英文:

Given a carriage return delimited text file say of folder names, how do I read this file into List<String> in the Cake build system for C#?

When I try to use the File class, it complains that File(string) is a method, which is not valid given the context:

filePath filePath;
string fileContents = File.ReadAllText(filePath FullPath);

How would I write this so it works with Cake?

https://cakebuild.net/

答案1

得分: 3

Cake有自己的File类,因此存在一个命名空间冲突,编译器不知道你要使用哪个File类。

你必须在调用中完全限定类型名称,包括System.IO命名空间:

string fileContents = System.IO.File.ReadAllText(filePath FullPath);

你还可能对Cake.FileHelpers插件感兴趣,该插件为Cake添加了一组别名,以帮助进行简单的文件操作,如读取、写入和替换文本。

英文:

Cake has its own File class, so there's a namespace conflict where the compiler doesn't know which File class you want to use.

You have to fully-qualify the type name in your call, to include the System.IO namespace:

filePath filePath;
string fileContents = System.IO.File.ReadAllText(filePath FullPath);

You might also be interested in the Cake.FileHelpers addin which adds a set of aliases for Cake to help with simple file operations such as Reading, Writing and Replacing text.

答案2

得分: 1

你可以使用 File.ReadAllLines()File.ReadLines()

这两者都不返回 List&lt;string&gt;,但都创建与 foreach 循环和 Linq 扩展兼容的对象,包括 .ToList()(如果需要的话)。

我还看到这个:

> 当我尝试使用 File 类时,它抱怨 File(string) 是一个方法,不在给定的上下文中有效:

这里有两种可能性。一种是,除了 System.IO.File,你可能在相同的作用域中还有另一个 File 类型或 File 方法。如果这是问题,那么请删除代码文件顶部可能有的任何 using System.IO 指令,并完全限定任何对 System.IO.File 的使用。

第二种可能性与代码片段有关,在两行上重复出现的 filePath filePathfilePath fullPath 都不太有意义,并表明可能缺乏基本语法的理解。

英文:

You want File.ReadAllLines() or File.ReadLines().

Neither of these returns a List&lt;string&gt;, but both create objects that work with foreach loops and Linq extensions, including .ToList() if you need it.

I also see this:

> When I try to use the File class, it complains that File(string) is a method, which is not valid given the context:

There are two possibilities here. One is, in addition to System.IO.File, you either have another File type or a File method in the same scope. If this is the issue, then remove any using System.IO directive you may have at the top of the code file and fully-qualify any use of System.IO.File.

The second possibility relates to the code snippet, with the repeating filePath filePath or filePath fullPath on both lines. Neither really makes sense at all, and indicates there might be a lack of understanding for basic syntax.

huangapple
  • 本文由 发表于 2023年3月4日 03:31:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定