【发布时间】:2013-06-17 21:21:21
【问题描述】:
public int RunStageData(string rootDirectory, stringdataFolder)
{
string[] files = new string[] { };
files = Directory.GetFiles(rootDirectory + dataFolder);
string[] tableOrder = new string[] { };
tableOrder = Directory.GetFiles(@"C:\_projects\ExampleProject\src", "TableOrder.txt");
System.IO.StreamReader tableOrderReader = new System.IO.StreamReader(tableOrder[0]);
for (int count = 0; count < files.Length; count++)
{
string currentTableName =tableOrderReader.ReadLine();
//files[count] = Directory.GetFiles(@"C:\_projects\ExampleProject\src", currentTableName);
}
}
大家好,对不起,如果我的代码有点草率。我的问题主要是我注释掉的那行。所以基本上我在这里要做的是根据这些名称在 txt 文件中的顺序填充文件名的字符串数组。所以我从 txt 文件中读取了第一行,然后在目录中检索该文件的名称(假设它存在)并将其放在数组的第一个位置,然后继续。
例如,如果 txt 文件按以下顺序包含这些单词:
- 狗
- 羊
- 猫
我希望数组首先包含 Dog,然后是 Sheep,然后是 Cat。我的问题是,我评论的那行给了我一个错误,上面写着“错误 41 无法将类型 'string[]' 隐式转换为 'string'”
我猜这是因为 Directory.GetFiles 有可能返回多个文件。那么,我可以使用另一种方法来实现我正在寻找的结果吗?谢谢。
【问题讨论】:
-
你为什么要创建无用的
new string[] { }s? -
错误告诉你为什么你得到错误你不能将字符串[]数组转换为
string你最好将文件读入字符串List<string>的通用对象并从那里排序.. -
好吧,看看它我不知道为什么 tableOrder 是一个字符串数组,但我不知道我是否看到它与我的问题有什么关系。
-
一般情况下,如果方法名是复数,返回的是某种数组
-
tableOrder是一个字符串数组,因为GetFiles会返回一个名称数组,即使只有一个文件与该模式匹配。您的第二次通话也是如此。