Popup dialog from server to remote computer in local network - c#

I'm trying to popup a dialog with some questions from a local network server
and receive the users answers, something like a winforms window, using c#.
Are there any recommendations for a way of doing this?

The solution I found was using PsExec tools.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "path of psExec",
Arguments = $"psexec -i -s \\\\{machineName} {path}",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.WaitForExit();`

Related

Programmatically execute commands in Windows Terminal using wsl

I am trying to run a python program in Ubuntu using wsl. I need to access Windows Terminal from C# application and execute commands in wsl. I am able to open the Windows Terminal. But cannot programmatically execute multiple commands after it.
I tried the below code. But the StandardInput.WriteLine is not working as I expected.
StartInfo = new ProcessStartInfo
{
FileName = #"wt.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = false,
Arguments = "wsl"
}
})
{
proc.Start();
proc.BeginOutputReadLine();
proc.StandardInput.WriteLine("cd Ubuntu/MyProject");
proc.StandardInput.WriteLine("python3 MyProgram.py ABC.wav");
}

Start process with IO stream redirection

How to start process and run command like this:
mysql -u root --password="some-password" < "some-file.sql"
Is it possible to do with process.Start()?
I need cross-platform solution (we cannot use cmd.exe).
Yes, this is possible through the System.Diagnostics.Process class. You need to set RedirectStandardInput to true, after which you can write the content of a file redirect the standard input of a process, and write the contents of the file to the Process.StandardInput (which is a StreamWriter)
This should get you started:
var process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "mysql.exe", // assumes mysql.exe is in PATH
Arguments = "-u root --password=\"some-password\"",
RedirectStandardInput = true,
UseShellExecute = false
},
};
process.Start();
process.StandardInput.Write(File.ReadAllText("some-file.sql"));
Update: this is pretty well documented [here](
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.redirectstandardinput)

Calling WSL bash.exe from C#

Mostly just as a curiosity, I wrote a little app to start up Terminator shell on Windows, using Ubuntu/WSL and Xming window server.
Doing things manually from the shell, I can run Firefox, gedit, Terminator, etc on Windows, it's pretty cool.
So I checked the location of bash.exe using where bash and it returned...
C:\Windows\System32\bash.exe
However when I tried to run this code...
using (var xminProc = new Process())
{
xminProc.StartInfo.FileName = #"C:\Program Files (x86)\Xming\Xming.exe";
xminProc.StartInfo.Arguments = ":0 -clipboard -multiwindow";
xminProc.StartInfo.CreateNoWindow = true;
xminProc.Start();
}
using (var bashProc = new Process())
{
bashProc.StartInfo.FileName = #"C:\Windows\System32\bash.exe";
bashProc.StartInfo.Arguments = "-c \"export DISPLAY=:0; terminator; \"";
bashProc.StartInfo.CreateNoWindow = true;
bashProc.Start();
}
I get the error...
System.ComponentModel.Win32Exception: 'The system cannot find the file specified'
And checking my entire system for bash.exe reveals it really be in another place altogether...
I'm not sure if this location is one that I can rely on, I'm worried it's ephemeral and can change during a Windows Store update, although I may be wrong about that.
Why does the command prompt show bash.exe to be in System32 but it's really in another location altogether?
Can I get C# to also use the System32 location?
As #Biswapriyo stated first set the platafrom to x64 on your solution:
Then you may run on your ubuntu machine from c# as:
Console.WriteLine("Enter command to execute on your Ubuntu GNU/Linux");
var commandToExecute = Console.ReadLine();
// if command is null use 'ifconfig' for demo purposes
if (string.IsNullOrWhiteSpace(commandToExecute))
{
commandToExecute = "ifconfig";
}
// Execute wsl command:
using (var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = #"cmd.exe",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true,
}
})
{
proc.Start();
proc.StandardInput.WriteLine("wsl " + commandToExecute);
System.Threading.Thread.Sleep(500); // give some time for command to execute
proc.StandardInput.Flush();
proc.StandardInput.Close();
proc.WaitForExit(5000); // wait up to 5 seconds for command to execute
Console.WriteLine(proc.StandardOutput.ReadToEnd());
Console.ReadLine();
}

Cmd not elevating when redirecting input/output

I'm trying to run automated installations via CMD commands. The programs output progress and I need to capture that output and calculate total progress in a nice window. In my understanding it is impossible to elevate and redirect at the same time. I've tried...
Running cmd elevated and feeding it commands.
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
proc.StandardInput.WriteLine("command");
Running cmd with command as an argument
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C " + "command",
Verb = "runas",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
CreateNoWindow = true
}
};
proc.Start();
To no effect. Is there any tricks to this? Elevating after input/output has been captured? I need this to work. Would this be possible with psexec?
If you own the installer programs, you can rewrite them so that they do not use standard streams as a means of communication, but for example named pipes. If you don't, you can write a wrapper program that runs elevated (you run it with UseShellExecute), does not use the standard streams, but runs the installer programs and redirects their input (you run them without UseShellExecute) reporting progress to the nonelevated main program via named pipes or some other means. I hope this diagram makes it clearer:
non-elevated.exe
\
\ named pipes
\
elevated-wrapper.exe (ran with UseShellExecute=true)
\
\ redirected standard streams
\
installer-program.exe (ran with UseShellExecute=false and redirected streams)

Programatically read content from terminal emulator

I'm trying to automate some operations in Mainframe. For that I made a C# program that connects to Mainframe using terminal emulator wc3270 and send keys to it.
This part works fine.
My problem is that I need to read the content displayed in terminal emulator screen so I can take better decisions about which keys I'll send to it.
How could I do it? I didn't find out any API that would let me do it.
Thanks.
I suggest using IBM personal communication tools:
http://www-03.ibm.com/software/products/en/pcomm
Together with the C# EHLLAPI wrapper, URL to example:
https://www.codeproject.com/Articles/9615/Using-EHLLAPI-in-C
To find out the session key for the emulator, you can use handle.exe from Windows Sysinternals
Code that im using for handle:
public String getSessionKey(String pid)
{
Process p = new Process();
p.StartInfo = new ProcessStartInfo()
{
UseShellExecute = false,
CreateNoWindow = true,
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = "/C C:\\handle.exe -a -p " + pid,
RedirectStandardError = true,
RedirectStandardOutput = true
};
p.Start();
String str = (p.StandardOutput.ReadToEnd());
String[] arr = str.Split('\n');
foreach (String s in arr)
{
if (s.Contains("Owned"))
{
return s.Substring(s.Length - 3, 1);
}
}
return "";
}

Categories