简体   繁体   English

如何将System.Collections.Specialized.StringCollection的内容写入文件?

[英]How to write contents of System.Collections.Specialized.StringCollection to file?

I have had no luck searching the obvious places for an answer to why using File.WriteAllLines(); 我没有运气搜索显而易见的地方,以回答为什么使用File.WriteAllLines(); doesn't work when outputting a StringCollection: 输出StringCollection时不起作用:

static System.Collections.Specialized.StringCollection infectedLog = new System.Collections.Specialized.StringCollection();

Omitting code here that has populated infectedLog....... 在这里省略已填充的感染日志代码.......

File.WriteAllLines(@"C:\CustomSearchInfectedFiles.txt", infectedLog);

Could anyone either tell me what I am doing wrong, or point me in the direction of an explanation that will please? 任何人都可以告诉我我做错了什么,或者指出一个令人满意的解释方向?

The File.WriteAllLines expects an IEnumerable<string> (or a string[] ) whereas StringCollection only implements IEnumerable (note the lack of generic type). File.WriteAllLines需要一个IEnumerable<string> (或一个string[] ),而StringCollection只实现IEnumerable (注意缺少泛型类型)。 Try the following: 请尝试以下方法:

using System.Linq;
...
File.WriteAllLines(@"C:\CustomSearchInfectedFiles.txt", infectedLog.Cast<string>());

尝试这个

File.WriteAllLines(@"C:\CustomSearchInfectedFiles.txt", infectedLog.Cast<string>());

The problem is that StringCollection is a really cold collection. 问题是StringCollection是一个非常冷的集合。 It does not implement IEnumerable<T>, and it is not an array, so there is no overload of WriteAllLines for it. 它没有实现IEnumerable <T>,并且它不是一个数组,所以WriteAllLines没有重载。

You can do this: 你可以这样做:

File.WriteAllLines(theFileName, infectedLog.Cast<string>());

Or, you could switch to a more modern collection type, like a List<string>. 或者,您可以切换到更现代的集合类型,如List <string>。

        using (StreamWriter w = File.AppendText(@"testfile.txt"))
        {
            foreach (var line in sc)
            {
                w.WriteLine(line);
            }
            w.Close();
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将System.Collections.Specialized.StringCollection类型转换为string [] - How to convert type System.Collections.Specialized.StringCollection to string[] 查找对象的类型,可能是System.Collections.Specialized.StringCollection - Find the type of the Object, probably System.Collections.Specialized.StringCollection System.Collections.Specialized.StringCollection 设置在调试和发布中工作正常但在部署时崩溃? - System.Collections.Specialized.StringCollection settings work fine in Debug and Release but crash on Deployment? .NET中的System.Collections,System.Collections.Specialized和System.Collections.Generic有什么区别? - What's the difference between System.Collections, System.Collections.Specialized, and System.Collections.Generic in .NET? System.Collections.Specialized.NameValueCollection.this [string] .get返回null - System.Collections.Specialized.NameValueCollection.this[string].get returned null 使用System.Collections.Specialized.BitVector32的问题:一个错误? - A problem with using System.Collections.Specialized.BitVector32: a bug? 无法从“字符串”转换为“ System.Collections.Specialized.NameValueCollection” - cannot convert from 'string' to 'System.Collections.Specialized.NameValueCollection' 无法将带有[]的索引应用于类型为&#39;System.Collections.Specialized.NameValueCollection&#39;的表达式 - Cannot apply indexing with [] to an expression of type 'System.Collections.Specialized.NameValueCollection' 如何将StringCollection转换为BindingList - How to convert StringCollection to BindingList 在模块System.dll中找不到类型System.Collections.Specialized.NameObjectCollectionBase - Cannot find type System.Collections.Specialized.NameObjectCollectionBase in module System.dll
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM