[英]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.