如何将 System.Collections.Specialized.StringCollection 的内容写入文件?
我没有运气在明显的地方寻找为什么使用 File.WriteAllLines(); 的答案。输出 StringCollection 时不起作用:
static System.Collections.Specialized.StringCollection infectedLog = new System.Collections.Specialized.StringCollection();
在这里省略已填充 InfectedLog 的代码......
File.WriteAllLines(@"C:\CustomSearchInfectedFiles.txt", infectedLog);
任何人都可以告诉我我做错了什么,或者为我指出一个令人满意的解释方向吗?
I have had no luck searching the obvious places for an answer to why using File.WriteAllLines(); doesn't work when outputting a 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
File.WriteAllLines
需要一个IEnumerable
(或string[]
),而StringCollection
仅实现 < code>IEnumerable (注意缺少泛型类型)。请尝试以下操作:The
File.WriteAllLines
expects anIEnumerable<string>
(or astring[]
) whereasStringCollection
only implementsIEnumerable
(note the lack of generic type). Try the following:试试这个
try this
问题是 StringCollection 是一个非常冷的集合。它没有实现 IEnumerable,并且它不是数组,因此没有 WriteAllLines 的重载。
您可以这样做:
或者,您可以切换到更现代的集合类型,例如 List。
The problem is that StringCollection is a really cold collection. It does not implement IEnumerable<T>, and it is not an array, so there is no overload of WriteAllLines for it.
You can do this:
Or, you could switch to a more modern collection type, like a List<string>.