cmd converting language other than English to junk - c#

I'm making a uiautomator based application where the app touches the objects based on text.
I've written the uiautomator methods in Java and have made a C# app which asks the used which text to touch. I'm using cmd/adb to execute the uiautomator commands.
The problem comes when I change the phone language to something other than English. Cmd/adb replaces the text (in other language) with ???. It passes the same to uiautomator.
Is there a way I can make cmd understand the other language?
C# code
Process procToExecute = new Process();
procToExecute.StartInfo.FileName = "adb.exe";
procToExecute.StartInfo.Arguments = strCommand;
procToExecute.StartInfo.CreateNoWindow = true;
procToExecute.StartInfo.UseShellExecute = false;
procToExecute.StartInfo.RedirectStandardOutput = true;
procToExecute.StartInfo.RedirectStandardInput = true;
Thread.Sleep(1500);
procToExecute.Start();
procToExecute.WaitForExit();
where strCommand looks like this for other languages
adb shell uiautomator runtest UiAutomatorTest.jar -c com.lge.uiautomatorTest.UiAutomatorClass#UiEnterText -e edit संदेश%दर्ज%करें -e text को
and it shows like this on cmd
adb shell uiautomator runtest UiAutomatorTest.jar -c com.lge.uiautomatorTest.UiAutomatorClass#UiEnterText -e edit ?????%????%???? -e text ??

Related

Use CMD commands in C# For showing Tasklist

I need to find the PID number for a process using C# and tasklist in the CMD.
the PID number needs to be put into a textbox in a c# form.
The code for finding the pid number in Command prompt is this.
for /f "tokens=1,2" %a in (' Tasklist /fi "imagename eq notepad.exe" /nh') do #echo %b
But I don´t know how to integrate CMD commands into C# winform.
You can achieve the same thing in .NET by using Process.GetProcessesByName and then outputting the process Id:
foreach (var p in Process.GetProcessesByName("notepad"))
{
Console.WriteLine(p.Id);
}
Alternatively, if you really want to use the cmd window and capture the output, you can create a process that will run cmd.exe and pass it the command line you want to execute (add a /C at the beginning, which tells cmd.exe to close the cmd window after running). You also want to RedirectStandardOutput, which allows you to capture the output of the command that was run. Then you can use proc.StandardOutput.ReadLine() to get each line that was returned:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/C for /f \"tokens=1,2\" %a in " +
"('Tasklist /fi \"imagename eq notepad.exe\" /nh') do #echo %b",
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
proc.Start();
proc.WaitForExit();
while (!proc.StandardOutput.EndOfStream)
{
Console.WriteLine(proc.StandardOutput.ReadLine());
}
You can certainly initiate a command prompt window ( Console ) and capture its stdout and stderr streams to grab the data and then parse it and show it in a windows forms control,
at the same time, usually in a case like this the Process class part of standard .NET Framework is used to retrieve any information of a running process on thw Windows machine,
have a look at this methos in MSDN for example: Process.GetProcessesByName or any other method of the Process class, that way you can do it without running any command in any console window.

File copy started from aspnet webforms and implemented using batch, pstools and robocopy doesn't work

I have 4 independent servers (not in domain):
IIS, SQL1, SQL2, SQL3
I want to copy a database backup from SQL1 to SQL2 or SQL3 (depending on parameters) by button click on webpage hosted on IIS
I wrote a button click method for that, which is calling batch file located in inetpub folder on IIS
Batch is using pstools to run robocopy on SQL1 which should copy required file to destination server (SQL2 or SQL3)
This solution works if I execute batch directly on IIS (cmd as Administrator) or when I debug it on my local machine, but it doesn't if it is called from the running site.
It even doesn't spend any time between the following lines:
batchProcess.Start();
batchProcess.WaitForExit();
Here is my copy method:
private bool ProcessCopy(string file, string destinationIp)
{
SecureString password = ConvertToSecureString("myPassword");
try
{
string batchPath = Server.MapPath(".") + "\\CopyFile.bat";
string cmd = #"c:\Windows\System32\cmd.exe";
ProcessStartInfo processInfo = new ProcessStartInfo
{
FileName = cmd,
UseShellExecute = false
};
Process batchProcess = new Process {StartInfo = processInfo};
batchProcess.StartInfo.Arguments = $"/C {batchPath} {file} {destinationIp}";
batchProcess.StartInfo.Domain = "";
batchProcess.StartInfo.UserName = "Administrator";
batchProcess.StartInfo.Password = password;
batchProcess.StartInfo.RedirectStandardOutput = true;
batchProcess.StartInfo.RedirectStandardError = true;
batchProcess.StartInfo.CreateNoWindow = true;
batchProcess.Start();
batchProcess.WaitForExit();
string response = batchProcess.StandardOutput.ReadToEnd();
response += batchProcess.StandardError.ReadToEnd();
statusStringAppend($"response: {response}");
return true;
}
catch (Exception ex)
{
statusStringAppend($"Failed: {ex.Message}. {ex.StackTrace}");
}
return false;
}
Batch body is:
#echo off
c:\qa\tools\pstools\psexec64.exe -accepteula -u Administrator -p myPassword \\SourceIP robocopy \\SourceIP\qa\db_backup\ \\%2\qa\db_backup\ %1 /is
My questions are:
1. Why the file was not copied?
2. Is there any better way to get it copied?
CODE UPDATED ACCORDING TO SUGGESTIONS BELOW
My guess is that you never executed pstools as the user that your IIS service is running as before, so the EULA dialog is blocking your execution.
If you remember, you always got a window and needed to press the accept button when running any sysinternals tool like pstools the first time.
I guess this should work for you:
c:\qa\tools\pstools\psexec64.exe -accepteula -u Administrator -p myPassword \\SourceIP robocopy \\SourceIP\qa\db_backup\ \\%2\qa\db_backup\ %1 /is
[EDIT]
You would most likely have hit this problem later on, anyway it did not work for you, so i have to list what else could be wrong with your code:
starting a .bat file needs cmd.exe as mother process, you cannot just start a .bat file as process directly. Instead you can for example use another method than ProcessStartInfo that spawns the system default script interpreter automatically: Executing Batch File in C#
the process for executing batch files is "cmd.exe", first parameter "/C", second parameter the batch file you are executing
when executing typical commandline tools, you might consider reading the SDTOUT (standard output) of the process you are executing, like this: Capturing console output from a .NET application (C#)

How to execute multiple commands - C#

There are 2 PCs(server & node). The Selenium hub is up & running. The notifications are seen in its cmd window.
Now, I'm trying to set up another PC as a Selenium node. To do that I need to run 2 commands from the server PC command prompt.It works when done manually.Failing to do so programatically.
Here is what I have so far.
private static void StartSeleniumNode()
{
string Command1 = "/C cmdkey.exe /add:ABCDES181 /user:abc /pass:abc#123 & ";
string Command2 = "psexec.exe \\ABCDES181 -i -w D:\\Selenium java -jar selenium-server-standalone-2.47.1.jar -role node -hub http://someip:4444/grid/register";
Process.Start(cmd.exe, Command1 + Command2);
}
When run, a cmd window just pops up and closes. There would be a notification if a node is registered, but nothing of that sort here. I think it is the syntax to run 2 commands that is the issue here.
The way to tell cmd to run multiple commands is to chain them using &&.
For ex, you could get your command prompt to do this:
echo hello && echo world
In your case, try using this statement:
Process.Start(Constants.CommandPrompt, string.Format("{0} && {1}", Command1,Command2));

How do I run a Python script from command line in a WPF application

I am trying to run a Python script from cmd line in a WPF application. I have this code which opens cmd line to the correct directory:
string cmd = #"webplus_builder.py";
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.WorkingDirectory = #"C:\Temp\WebBuilder";
p.StartInfo.Arguments = string.Format("{0} {1} {2}", cmd, "WAF", _CountryCode);
p.StartInfo.UseShellExecute = false;
p.Start();
I now want to run webplus_builder.py from that directory like so:
How do I achieve this? Is it a case of just adding arguments to my Process as I've attempted to do?
You cannot execute python scripts directly, it's python itself that has to be run: Use the name of your script as the first parameter to it, the following parameters are passed to your script.
(Windows handles a double click on py-files by "opening them with" python - exactly as described).
Read the official docs for details:
1. Command line and environment — Python 2.7.9 documentation
1. Command line and environment — Python 3.3.6 documentation

Why can't i use this code to make a minecraft launcher in c#?

I have searched everywhere to find out how to make a custom minecraft launcher. I managed to create this code, which should work, but sadly it does not. I login but it never starts, however for a second I get the loading ring next to my mouse. This is my code:
ProcessStartInfo start = new ProcessStartInfo();
// Enter in the command line arguments, everything you would enter after the executable name itself
start.Arguments = #"-Xmx1G -Djava.library.path=%APPDATA%\.minecraft\versions\1.6.2\1.6.2-natives -cp %APPDATA%\.minecraft\libraries\net\sf\jopt-simple\jopt-simple\4.5\jopt-simple-4.5.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecjorbis\20101023\codecjorbis-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\codecwav\20101023\codecwav-20101023.jar;%APPDATA%\.minecraft\libraries\com\paulscode\libraryjavasound\20101123\libraryjavasound-20101123.jar;%APPDATA%\.minecraft\libraries\com\paulscode\librarylwjglopenal\20100824\librarylwjglopenal-20100824.jar;%APPDATA%\.minecraft\libraries\com\paulscode\soundsystem\20120107\soundsystem-20120107.jar;%APPDATA%\.minecraft\libraries\argo\argo\2.25_fixed\argo-2.25_fixed.jar;%APPDATA%\.minecraft\libraries\org\bouncycastle\bcprov-jdk15on\1.47\bcprov-jdk15on-1.47.jar;%APPDATA%\.minecraft\libraries\com\google\guava\guava\14.0\guava-14.0.jar;%APPDATA%\.minecraft\libraries\org\apache\commons\commons-lang3\3.1\commons-lang3-3.1.jar;%APPDATA%\.minecraft\libraries\commons-io\commons-io\2.4\commons-io-2.4.jar;%APPDATA%\.minecraft\libraries\net\java\jinput\jinput\2.0.5\jinput-2.0.5.jar;%APPDATA%\.minecraft\libraries\net\java\jutils\jutils\1.0.0\jutils-1.0.0.jar;%APPDATA%\.minecraft\libraries\com\google\code\gson\gson\2.2.2\gson-2.2.2.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl\2.9.0\lwjgl-2.9.0.jar;%APPDATA%\.minecraft\libraries\org\lwjgl\lwjgl\lwjgl_util\2.9.0\lwjgl_util-2.9.0.jar;%APPDATA%\.minecraft\versions\1.6.2\1.6.2.jar net.minecraft.client.main.Main --username playername --session token:"+ words[3] + #":" + words[4]+ #" --version 1.6.2 --gameDir %APPDATA%\.minecraft --assetsDir %APPDATA%\.minecraft\assets";
start.FileName = #"c:\Program Files (x86)\java\jre7\bin\javaw.exe";
// Do you want to show a console window?
start.CreateNoWindow = true;
System.Diagnostics.Process.Start(start);
This just does the loading ring by my mouse for a second, then nothing opens. No logs, crashes, errors, nothing wrong. This is Visual c# compiled on Visual Studio 2012.
The arguments you are giving have an environment variable in them - %APPDATA%.
The command line will expand this by default, but the .net library won't.
See How do I ensure c# Process.Start will expand environment variables?
As Pete Kirkham mentioned you need to set up environment variable.
You can set it before starting the Process like:
var appDataPath = "your path";
start.EnvironmentVariables.Add("APPDATA", appDataPath);

Categories

Resources