【问题标题】:Failed to convert xml to csv using perl script through c#无法通过 c# 使用 perl 脚本将 xml 转换为 csv
【发布时间】: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 answerRunning Perl throughC# code。但除此之外,我们还需要查看minimal reproducible example,您对文件路径等进行了大量操作,您确定目录和文件确实存在于指定位置吗?尝试在调试器中调用File.Exists(string)Directory.Exists(string) 确定。

标签: c# windows perl


【解决方案1】:

首先,找出问题所在:

string error = perl.StandardError.ReadToEnd();

此外,请确保您拥有在输出目录中创建文件所需的权限。您可以尝试以管理员权限运行您的进程,以确定是否是权限问题:

perlStartInfo.Verb = "runas";

您可能希望为此提升权限来运行整个主机进程。

(这只是为了弄清楚是否是权限问题!如果是这种情况,请授予输出目录必要的权限,如果可能,请不要在生产环境中自动运行具有管理员权限的脚本) em>

perl 脚本本身也可能有错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-23
    相关资源
    最近更新 更多