作为背景:我有以下代码通过目录递归搜索文件,该目录返回所有文档的列表。我想要文件夹名称和文档名称。 到现在为止还挺好。只要我想向列表中添加两个元素,它就可以很好地工作。
我有一个Companyentry类;
class Companyentry
{
public string Entry { get; set; }
public string Folder { get; set; }
public Companyentry(string ey, string fd)
{
Entry = ey;
Folder = fd;
}
}
我要写入列表的代码:
List<string> companylist = new List<string>();
private async void button4_Click(object sender, EventArgs e)
{
// ListView initialisierem
listView1.Columns.Add("Dateiname", 200, HorizontalAlignment.Left);
listView1.Columns.Add("Unternehmen", 20, HorizontalAlignment.Left);
listView1.CheckBoxes = true;
listView1.Sorting = SortOrder.Ascending;
DirectoryInfo di = new DirectoryInfo(rootfolder);
Console.WriteLine("No search pattern returns:");
//Write to List
foreach (var fi in di.GetFiles("*", SearchOption.AllDirectories))
{
// Entries
string[] Split = (fi.Directory.Name).Split(new Char[] { '_' });
strEntry = (Split[1]);
companylist.Add(new Companyentry("Entry", "Folder"));
}
我收到以下错误消息:“参数1无法从Companyentry转换为'string'”:
怎么了?
答案 0 :(得分:0)
因为这一行是错误的:
companylist.Add(new Companyentry("Entry", "Folder"));
更改为此:
companylist.Add("Entry", "Folder");
new Companyentry("Entry", "Folder");
列表是一个字符串列表。