C# File.ReadLines和convert类型错误
使用C# File.ReadLines和convert类型错误,c#,file,C#,File,使用System.IO然后使用此代码写入Txt文件 Text.Text = File.ReadLines(Path.Text, Encoding.Unicode); Text是包含文件路径的文本框的名称。 错误是: Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string' 无法将类型“System.Collections.Generic.
System.IO
然后使用此代码写入Txt文件
Text.Text = File.ReadLines(Path.Text, Encoding.Unicode);
Text是包含文件路径的文本框的名称。
错误是:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<string>' to 'string'
无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“string”
试试这个:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines(Path.Text, Encoding.Unicode);
System.Collections.Generic.IEnumerable lines=File.ReadLines(Path.Text,Encoding.Unicode);
请注意,
File.ReadLines()
返回类型为System.Collections.Generic.IEnumerable
的对象,因为您试图将字符串集合(IEnumerable
)的值分配给字符串属性(Text
)
使用以下命令:
Text.Text = File.ReadAllText(Path.Text, Encoding.Unicode)
ReadAllText
将整个文件内容作为单个字符串返回。file.ReadLines()
返回类型为System.Collections.Generic.IEnumerable
的对象。
File.ReadAllLines()
返回字符串数组
如果要使用字符串数组,则需要调用正确的函数
为便于解决,只需使用ReadAllLines()
,或者您可以更改返回类型
这也将有助于:
System.Collections.Generic.IEnumerable<String> lines = File.ReadLines("c:\\file.txt");
System.Collections.Generic.IEnumerable lines=File.ReadLines(“c:\\File.txt”);
可以使用实现IEnumerable的任何泛型集合。举个例子。试试
File.ReadAllText
。