【发布时间】:2019-06-28 22:58:36
【问题描述】:
我有以下代码用于使用 perl 脚本将 xml 转换为 csv。当我通过 c# 运行 perl 脚本但没有创建文件并且字符串输出变为空时。 问题是什么? 我有扩展名为 .txt 的 perl 脚本,这样可以吗?
string filePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/Files/SAVVIS_CDR_1806012231.XML";
if (Path.GetExtension(filePath) != "csv")
{
ProcessStartInfo perlStartInfo = new ProcessStartInfo(@"C:\Strawberry\perl\bin\perl.exe");
string perlScriptFilePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/PerlScript/formatter_savvis.pl.txt";
string csvFilePath = Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + "/PerlScript/";
perlStartInfo.Arguments = perlScriptFilePath + " " + filePath + " " + csvFilePath;
perlStartInfo.UseShellExecute = false;
perlStartInfo.RedirectStandardOutput = true;
perlStartInfo.RedirectStandardError = true;
perlStartInfo.CreateNoWindow = false;
Process perl = new Process();
perl.StartInfo = perlStartInfo;
perl.Start();
perl.WaitForExit();
string output = perl.StandardOutput.ReadToEnd();
}
你能请任何人帮我解决这个问题吗? 提前致谢。
【问题讨论】:
-
你指的是什么问题?
-
为什么用perl标记?我没有看到任何 Perl 代码,只有 Windows 进程处理
-
@StefanBecker 我添加了
perl标签,但公平地说,我还添加了c#标签。 OP 似乎不知道发生了什么或他们的问题到底是什么。 -
虽然我很喜欢 perl...如果你使用的是 C#,为什么不直接用那种语言来做呢?
-
您可能需要颠倒对
perl.WaitForExit();和perl.StandardOutput.ReadToEnd();的调用顺序——有关详细信息,请参阅this answer 到Running Perl throughC# code。但除此之外,我们还需要查看minimal reproducible example,您对文件路径等进行了大量操作,您确定目录和文件确实存在于指定位置吗?尝试在调试器中调用File.Exists(string)和Directory.Exists(string)确定。