繁体   English   中英

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

[英]Looping a directory to process multiple XML files

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

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);
}

任何建议都将非常感激。

看起来你实际上并没有在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);

试试这个:

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

循环上的一个小错误,你需要用file而不是path调用ReadAllBytes

byte[] byteArray = System.IO.File.ReadAllBytes(file);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM