-1

I have the following code for converting xml to csv by using perl script. When I run the perl script through c# but there is no files are created and string output become empty. What is the problem? I have the perl script with .txt extention, Is this ok or not?

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();
            }

Could you please anyone help me to solve this problem? Thanks in advance.

5
  • What problem are you referring to? Commented Feb 5, 2019 at 7:07
  • Why is this tagged with perl? I don't see any Perl code, just Windows process handling Commented Feb 5, 2019 at 7:11
  • 1
    @StefanBecker I added the perl tag, but to be fair, I also added the c# tag. OP doesn't seem to know what's going on or what their question actually is. Commented Feb 5, 2019 at 7:13
  • 1
    Much as I love perl... if you're using C#, why not do it directly in that language? Commented Feb 5, 2019 at 7:29
  • 3
    You may need to flip the order of the calls to perl.WaitForExit(); and perl.StandardOutput.ReadToEnd(); -- see this answer to Running Perl throughC# code for details. But beyond that we need to see a minimal reproducible example, you do a lot of manipulation of files paths and such, are you sure directories and files actually exist at the stated location(s)? Try calling File.Exists(string) and Directory.Exists(string) in the debugger to be sure. Commented Feb 5, 2019 at 7:54

1 Answer 1

1

First, to find out what went wrong:

string error = perl.StandardError.ReadToEnd();

Also, make sure you have necessary permissions to create files in the output directory. You may try to run your process with Admin privileges to find out if it's a permission issue:

perlStartInfo.Verb = "runas";

You may want to run your entire host process with elevated permissions for this.

(This is only to figure out if it's a permission issue! If it's the case, grant the output directory the necessary permissions, and if possible don't automatically run scripts with admin privileges in production environment)

There also may be errors in the perl script itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.