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