我必须检查不同目录数组中的每个xml文件。
我的代码(仍有错误):
string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
//Get each path and remove whitespaces
string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
//Use xmlLoc for adding \ to each file
List<string> xmlLoc = new List<string>();
//get the files in directories
string[] getFiles;
//contains the files of each directory
List<string> xmlList
//Add \ each paths variable and store it in xmlLoc list
foreach (string s in paths)
{
xmlLoc.Add(s + @"\");
}
//get the xml files of each directory in xmlLoc and store it in xmlList
foreach (string file in xmlLoc)
{
getFiles = Directory.GetFiles(file, "*.xml");
//the code below lists an error "cannot convert from string[] to string"
xmlList.Add(getFiles);
}
我猜你不能将数组存储在字符串列表中。我可以用其他任何方式读取存储在数组中的每个目录中的文件吗?
答案 0 :(得分:4)
您是否尝试过使用AddRange?
像
这样的东西xmlList.AddRange(getFiles);
从我所看到的情况来看,你也可能只是喜欢
List<string> xmlList = files.Split(new[] {';', ' '}, StringSplitOptions.RemoveEmptyEntries).
SelectMany(p => Directory.GetFiles(p, "*.xml")).
ToList();
答案 1 :(得分:2)
目前还不太清楚您要尝试做什么,但您可以使用AddRange
方法将string[]
返回的Directory.GetFiles
数组的所有元素一次性添加到列表中:
string[] getFiles = Directory.GetFiles(file, "*.xml");
xmlList.AddRange(getFiles);
还要考虑以下事项:
您的xmlList
实例未初始化,请尝试:List<string> xmlList = new List<string>
();
file
构造中变量foreach
的名称用词不当,请考虑使用directory
,因为这就是xmlLoc
的“元素”是。
您并不真正需要getFiles
变量,在您的情况下,简单的xmlList.AddRange(Directory.GetFiles(file, "*.xml"));
就足够了。
拆分空白并不是一个好主意。目录名称(尽管不是您使用的示例)可能自己包含空格。
您的代码看起来有点复杂。 AFAICT以下内容也是如此:
string directories = /* ... whatever ... */;
List<string> xmlList = new List<string>();
foreach (string directory in string.Split(new[] {';'}, StringSplitOptions..RemoveEmptyEntries))
{
string dir = directory.Trim();
if (!dir.EndsWith(Path.DirectorySeparator))
dir += Path.DirectorySeparator;
xmlList.AddRange(Directory.GetFiles(dir, "*.xml"));
}
答案 2 :(得分:1)
修正了它!只需要添加和替换一些代码.. :)
string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
//Get each path and remove whitespaces
string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
//Use xmlLoc for adding \ to each file
List<string> xmlLoc = new List<string>();
//get the files in directories
string[] getFiles;
//Add \ each paths variable and store it in xmlLoc list
foreach (string s in paths)
{
xmlLoc.Add(s + @"\");
}
//get the xml files of each directory in xmlLoc and loop it to read the files
foreach (string directory in xmlLoc)
{
getFiles = Directory.GetFiles(directory, "*.xml");
foreach(string files in getFiles)
{
MessageBox.Show(files);
}
}
答案 3 :(得分:0)
试试这个,你需要初始化XML列表,并且GetFiles返回一个数组,所以你需要调用AddRange,而不是在添加到XML列表时添加。
string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening";
//Get each path and remove whitespaces
string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries);
//Use xmlLoc for adding \ to each file
List<string> xmlLoc = new List<string>();
//get the files in directories
string[] getFiles;
//contains the files of each directory
List<string> xmlList = new List<string>();
//Add \ each paths variable and store it in xmlLoc list
foreach (string s in paths)
{
xmlLoc.Add(s + @"\");
}
//get the xml files of each directory in xmlLoc and store it in xmlList
foreach (string file in xmlLoc)
{
getFiles = Directory.GetFiles(file, "*.xml");
//the code below lists an error "cannot convert from string[] to string"
xmlList.AddRange(getFiles);
}