1

異なるディレクトリの配列内の各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);
}

文字列リストに配列を格納することはできないと思います。配列に格納されている各ディレクトリのファイルを読み取る方法は他にありますか?

4

4 に答える 4

4

AddRangeを使用してみましたか?

何かのようなもの

xmlList.AddRange(getFiles); 

私が見ることができることから、あなたはまた、次のようなもので行った可能性があります

List<string> xmlList = files.Split(new[] {';', ' '}, StringSplitOptions.RemoveEmptyEntries).
    SelectMany(p => Directory.GetFiles(p, "*.xml")).
    ToList();
于 2012-09-04T04:30:56.160 に答える
2

何をしようとしているのかは明確ではありませんが、このメソッドを使用して、返される配列AddRangeのすべての要素を一度にリストに追加できます。string[]Directory.GetFiles

 string[] getFiles = Directory.GetFiles(file, "*.xml");
 xmlList.AddRange(getFiles);

次のことも考慮してください。

  1. xmlListインスタンスが初期化されていません。試してください:( )List<string> xmlList = new List<string>;

  2. fileコンストラクト内の変数の名前はforeach誤った名前です。directory代わりに使用することを検討してください。これがの「要素」でxmlLocあるためです。

  3. 変数は実際には必要ありません。あなたの場合getFilesは単純な xmlList.AddRange(Directory.GetFiles(file, "*.xml"));もので十分です。

  4. 空白で分割するのは良い考えではありません。ディレクトリ名(使用する例ではありませんが)には、空白自体が含まれている場合があります。

コードは少し複雑に見えます。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"));
}
于 2012-09-04T04:31:27.997 に答える
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);
     }
}
于 2012-09-04T04:41:02.520 に答える
0

これを試してください。XMLリストを初期化する必要があります。GetFilesは配列を返すため、XMLリストに追加するときにAddではなくAddRangeを呼び出す必要があります。

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);
            }
于 2012-09-04T06:10:18.550 に答える