循环目录以处理多个XML文件

时间:2013-04-30 08:28:58

标签: c# sql xml database

我有一个客户端应用程序,它根据静态路径定位文件并相应地处理它:

string filepath = @"C:\Users\NChamber\Desktop\package\1002423A_attachments.xml";
byte[] byteArray = System.IO.File.ReadAllBytes(filepath);
channel.UploadTransaction(filepath, 27, byteArray);

这适用于单个文件更新,但我需要的是扫描整个目录中所有以“* .xml”结尾的文件并对其进行处理。

到目前为止,我已经尝试过这一点,但收效甚微:

string path = @"C:\Users\NChamber\Desktop\package\";

foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
{
    byte[] byteArray = System.IO.File.ReadAllBytes(path);
    channel.UploadTransaction(path, 27, byteArray);
}

任何建议都会非常感激。

3 个答案:

答案 0 :(得分:3)

看起来你实际上并没有在foreach循环中对file做任何事情,你只是在每次迭代时传入path

foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
{
    byte[] byteArray = System.IO.File.ReadAllBytes(path);
    channel.UploadTransaction(file, 27, byteArray);
}

我怀疑你的意思是:System.IO.File.ReadAllBytes(file);例如:

foreach (string file in Directory.EnumerateFiles(path, "*.xml"))
{
    byte[] byteArray = System.IO.File.ReadAllBytes(file);
    channel.UploadTransaction(file, 27, byteArray);
}

然后:channel.UploadTransaction(file, 27, byteArray);

答案 1 :(得分:2)

试试这个:

foreach (string file in Directory.GetFiles(path, "*.xml"))
{
byte[] byteArray = System.IO.File.ReadAllBytes(file);
channel.UploadTransaction(file, 27, byteArray);                        
}

答案 2 :(得分:2)

循环出现小错误,您需要使用ReadAllBytes而不是file致电path

byte[] byteArray = System.IO.File.ReadAllBytes(file);
相关问题