C#:循环访问目录调用外部进程
我有下面的 C# 代码,用于输入目录中的每个文件,对其运行 XQuery 进程,并将每个文件输出为 XML 文件。该代码调用 SAXON XQuery 处理器。注意:代码现在可以运行,但它只处理目录中的第一个文件。输入目录中的第二个、第三个等输出文件将作为空 XML 文件返回。问题:如何修改代码以处理输入目录中的所有文件(而不仅仅是第一个文件)?
public void OpenWithArguments(string t)
{
string sourceDir = t;
string [] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\Java\jdk6\bin\java";
process.StartInfo.CreateNoWindow = true;
process.StartInfoArguments =
@" -cp C:\mydir\saxon9he.jar net.sf.saxon.Query -o:C:\myOutPutFiles\" +
Path.GetFileNameWithoutExtension(fileName) +
@".xml C:\myQueries\myquery.xquery input=" +
Path.GetFileNameWithoutExtension(fileName);
process.Start();
process.Close();
}
}
I have the C# code below, used to input each file in a directory, run an XQuery process on it, and output each file as an XML file. The code calls the SAXON XQuery processor. Note: the code works now but it only processes the first file in the directory. The second, third, etc. output files from the input directory come back as empty XML files. Question: how do I modify the code to process all the files (not just the first one) in the input directory?
public void OpenWithArguments(string t)
{
string sourceDir = t;
string [] fileEntries = Directory.GetFiles(sourceDir);
foreach (string fileName in fileEntries)
{
Process process = new Process();
process.StartInfo.FileName = @"C:\Program Files\Java\jdk6\bin\java";
process.StartInfo.CreateNoWindow = true;
process.StartInfoArguments =
@" -cp C:\mydir\saxon9he.jar net.sf.saxon.Query -o:C:\myOutPutFiles\" +
Path.GetFileNameWithoutExtension(fileName) +
@".xml C:\myQueries\myquery.xquery input=" +
Path.GetFileNameWithoutExtension(fileName);
process.Start();
process.Close();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在启动后添加 process.WaitForExit() 。
Try adding process.WaitForExit() after you start it.