How to start a process using Xamarin C# on an android app? - c#

Having this code:
Process checkForUpdates = new Process();
checkForUpdates.StartInfo.FileName = #"python";
checkForUpdates.StartInfo.Arguments = "-V";
checkForUpdates.StartInfo.UseShellExecute = true;
checkForUpdates.StartInfo.RedirectStandardOutput = true;
checkForUpdates.StartInfo.RedirectStandardError = true;
checkForUpdates.StartInfo.CreateNoWindow = false;
checkForUpdates.EnableRaisingEvents = true;
checkForUpdates.Start();
string result = checkForUpdates.StandardOutput.ReadToEnd();
I'm trying retrieve the output of the command python -V in order to determine if python is installed on the device.
The code above compiles but the code seems to hang during the process without any errors. The same code works fine on UWP.
Is there any other way to make it work on Android?

The problem was that I run this code inside a background worker. I put the code in a method directly inside ViewModel and it worked perfectly fine.

Related

Can we run node application in net core 3.1

I am having a .Net Core 3.1 console application (which is work as a azure consumer for a message queue)
When executing the Consumer, based on a flag value of message property, Its need to run separate application which is written in node.js.
I decided use separate run.sh (shell script) file to call node application and run.sh file will call through c#.
Here is the code snippet I used.
var command = "bash";
var myBatchFile = #"C:\Temp\spiderease_v1\run.sh"; //Path to shell script file
var argss = #"C:\Temp\spiderease_v1\run.sh CA--STATCAN--REG--ADMIN-DATA {url} {OUTPUT_PATH} {SPIDER_EASE_HOME} {SPIDER_TEMPLATE_HOME} {TYPE}"; //this would become "/home/ubuntu/psa/PdfGeneration/ApacheFolder/ApacheFOP/transform.sh /home/ubuntu/psa/PdfGeneration/ApacheFolder/XMLFolder/test.xml /home/ubuntu/psa/PdfGeneration/ApacheFolder/XSLTFolder/Certificate.xsl /home/ubuntu/psa/PdfGeneration/ApacheFolder/PDFFolder/test.pdf"
var processInfo = new ProcessStartInfo();
processInfo.UseShellExecute = false;
processInfo.FileName = command; // 'sh' for bash
processInfo.Arguments = argss; // The Script name
processInfo.RedirectStandardOutput = true;
var process = Process.Start(processInfo); // Start that process.
var outPut = process.StandardOutput.ReadToEnd();
process.WaitForExit();
But This won't work as expected.
Update:
This code snipt didn't give any error, but I am unable to ensure that file getting executed or not. because when I run this bash file directly, it produced some files in given path. here it didn't give any output.
Can someone help me out whether do I using correct implementation for this task or do I have any alternatives?
If this is an good solution, what would be the wrong in code?
(I am willing push this into docker Linux environment as well)
Can someone help me out on this?

C# MVC Core Print Excel file using Process(), PDF works fine but Excel keep loading

I am trying to print Excel(.xlsx) file via my web application by clicking a button.
Everything works fine on my local, however when I upload to IIS, the moment I click the button. The page will just keep loading.
string destinationFileWord = filePath;
Process print = new Process();
print.StartInfo.FileName = destinationFileWord;
print.StartInfo.UseShellExecute = true;
print.StartInfo.CreateNoWindow = true;
print.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
print.StartInfo.Verb = "PrintTo";
print.StartInfo.Arguments = Utility.GetAppSettings("PrinterSettings", "Address1");
print.StartInfo.WorkingDirectory = Path.GetDirectoryName(destinationFileWord);
print.Start();
if (print.HasExited == false)
{
print.WaitForExit(5000);
}
Does anyone know what might be the problem?
Thank you.
Your issue is mostly likely the directory mapping. On the server you need to use something like this to get the current working directory.
HttpContext.Current.Server.MapPath("~/")
There are a number of other methods posted in this SO post:
How do I get the current directory in a web service
EDIT
For .NET Core you need to use IHostingEnvironment.ContentRootPath:
How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac

Console Input Inside WPF Application

I'm trying to get console input from inside a WPF application. In the Project properties I've set it as a console application and the output works. However, now I want to be able to read input from the console since I haven't started working on the views yet. I found this snippet on here, but I don't believe it's what I was looking for:
Process compiler = new Process();
compiler.StartInfo.FileName = "C:\\Windows\\Microsoft.NET\\Framework64\\v4.0.30319\\csc.exe";
compiler.StartInfo.Arguments = "/r:System.dll /out:sample.exe stdstr.cs";
compiler.StartInfo.UseShellExecute = false;
compiler.StartInfo.RedirectStandardOutput = true;
compiler.Start();
Console.WriteLine(compiler.StandardOutput.ReadToEnd());
compiler.WaitForExit();
Why not simply use Console.ReadLine()?
string userInput = Console.ReadLine();

Problem redirecting console in C#

i am building a GUI over an console application that i use, i'm trying to use C# Process and RedirectStandardInput/Error/Output.
The problem is when i redirect anything the console applications stops printing data, i've redirected only stdin and watched the console application window itself, nothing, i've redirected stdin, out and err and no data is received by my program.
This is the only application i tested that doesn't work with Std redirection, is it an implementation issue?
Code:
fmproc = new Process();
fmproc.StartInfo.FileName = #"fm.exe";
fmproc.StartInfo.Arguments = "";
fmproc.StartInfo.UseShellExecute = false;
fmproc.StartInfo.CreateNoWindow = false;
fmproc.StartInfo.RedirectStandardInput = true;
fmproc.StartInfo.RedirectStandardError = true;
fmproc.StartInfo.RedirectStandardOutput = true;
fmproc.Start();
fmproc.StandardOutput.ReadLine();
Perhaps you could try:
fmproc.StandardOutput.ReadToEnd();
fmproc.WaitForExit();
Rather than the ReadLine(); Of course you might want to do something more interesting with the output string - but I'm just following your example code.

Perl Script Output Capture Problem using C#

I was following one of the thread to run perl scripts from my c# program.
My c# code is like this:
private void RunScript(ArrayList selectedScriptFileList)
{
foreach (var curScriptFileName in selectedScriptFileList)
{
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
myProcessStartInfo.Arguments = (string)(curScriptFileName);
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
myProcess.WaitForExit();
string output = myProcess.StandardOutput.ReadToEnd();
this.ScriptTestResultTextBox.AppendText(output);
}
}
And my perl script requires XML parsing. I can read the print statement before the XML parsing, but not after the parsing starts. The script runs find on DoS shell.
Here is part of my script:
print("\n");
print("****************** test1.pl ***********************\n");
print("\n");
print("1");
print("2");
my $scriptName = 'test1.pl';
my $file = '../../ScriptParamLib.xml';
my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($file);
my $root = $tree->getDocumentElement;
my #species = $root->getElementsByTagName('test_node');
print("Accessing XML Data Base...\n");
The c# testbox only shows the first three print statement but not the last one.
Does anybody knows why?
Thanks
You could add more debugging print statements (e.g. one between every other line of your code) to see how far the execution gets. However, I'm going to go on a hunch and suggest that adding these three lines to your script will either solve the problem outright or lead you closer to a solution:
use strict;
use warnings;
use XML::LibXML;
Please update your question indicating how far execution gets and what errors you see!
I figured I should roll my comments into an answer since they proved to be helpful:
Since using an absolute path for $file in the Perl script works, the issue most likely has something to do with the working directory of the process that gets spawned from the C# program. You can use the Cwd module in the Perl script to see what the working directory actually is. If it's not what you expect, try setting it via the WorkingDirectory property of ProcessStartInfo in your C# program. Relative paths should work just fine after that.

Categories