i am use this code in order to open all link from specific URL, each link will open with new tab, this cause a huge memory use, how can i open the new link on the existing Tab ?
static void Main(string[] args)
{
ProcessStartInfo processStartInfo = null;
string googleChoromePath = "C:\\Users\\Dandin\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe";
string argument = "";
string url = "http://edition.cnn.com/";
WebClient wClient = new WebClient();
string st = wClient.DownloadString(url);
List<string> list = LinkFinder.Find(st);
processStartInfo = new ProcessStartInfo(googleChoromePath);
for (int i = 0; i < list.Count; i++)
{
argument = list[i];
try
{
if (argument.StartsWith("http://"))
{
processStartInfo.Arguments = argument;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.RedirectStandardError = true;
processStartInfo.CreateNoWindow = true;
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
Process googleChrome = Process.Start(processStartInfo);
Thread.Sleep(1000);
}
}
catch (Exception)
{
}
}
}
Related
class TcpIpCommands
{
public string Check()
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Windows\syswow64\netsh.exe";
p.StartInfo.Arguments = "int tcp show global";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(737);
p.Start();
p.WaitForExit();
return p.StandardOutput.ReadToEnd();
}
public string Command(string FileName, string Arguments)
{
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = FileName;
p.Arguments = Arguments;
p.UseShellExecute = false;
p.CreateNoWindow = true;
p.RedirectStandardOutput = true;
p.StandardOutputEncoding = Encoding.GetEncoding(737);
using (Process process = Process.Start(p))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
return result;
}
}
}
public string Reseting()
{
Process p = new Process();
p.StartInfo.FileName = #"C:\Windows\syswow64\netsh.exe";
p.StartInfo.Arguments = #"interface tcp reset";
p.StartInfo.Verb = "runas";
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.StandardOutputEncoding = Encoding.GetEncoding(737);
p.Start();
p.WaitForExit();
return p.StandardOutput.ReadToEnd();
}
}
I have an problem with Command Method and Reseting Method. If i try to execute them I receive in RichTextBox the message
set global command failed on ipv4 the parameter is incorrect
But If i run netsh interface tcp show global or hit the Check Method they info me that the status are change.
here are the buttons
private void BtnCheck_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Check();
}
private void btnChimney_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Command(#"C:\Windows\system32\netsh.exe", "interface tcp set global chimney=enabled");
}
private void BtnReset_Click(object sender, EventArgs e)
{
TcpIpCommands tic = new TcpIpCommands();
richTextBox1.Text = tic.Reseting();
}
I cant understand why RichTextBox shows that message but the process execute succefully.
A note. I have try to run the commands with
StartInfo.Verb = "runas";
and both from
C:\Windows\System32\netsh.exe
and
C:\Windows\syswow64\netsh.exe
and i get the same error "set global command failed on ipv4 the parameter is incorrect" but the code execute and the parameter change.
I have been trying to read process output for parsing morphological analysis. But I can't read pckimmo32.exe output.
public static string Problem1()
{
ProcessStartInfo _startInfo = new ProcessStartInfo();
Process p = new Process();
StringBuilder outputStringBuilder = new StringBuilder();
string filePath = AppDomain.CurrentDomain.BaseDirectory + #"..\..\PC-KIMMO\pckimmo32.exe";
var file = new FileInfo(filePath);
p.StartInfo = _startInfo;
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
_startInfo.RedirectStandardInput = true;
_startInfo.WorkingDirectory = file.Directory.FullName;
_startInfo.FileName = file.FullName;
p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
p.Start();
p.BeginOutputReadLine();
var myWriter = p.StandardInput;
myWriter.AutoFlush = true;
myWriter.WriteLine("synthesize kitap +Noun +A3sg +P2sg +Loc");
myWriter.Close();
p.WaitForExit();
var output = outputStringBuilder.ToString();
return output;
}
public static void Display(DataReceivedEventArgs nes)
{
Console.WriteLine(nes.Data);
}
I can read another text exe file output.
public static string Problem2()
{
ProcessStartInfo _startInfo = new ProcessStartInfo();
Process p = new Process();
StringBuilder outputStringBuilder = new StringBuilder();
string filePath = AppDomain.CurrentDomain.BaseDirectory + #"..\..\..\RTest\bin\debug\RTest.exe";
var file = new FileInfo(filePath);
p.StartInfo = _startInfo;
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
_startInfo.RedirectStandardInput = true;
_startInfo.WorkingDirectory = file.Directory.FullName;
_startInfo.FileName = file.FullName;
p.OutputDataReceived += (sender, eventArgs) => outputStringBuilder.AppendLine(eventArgs.Data);
p.Start();
p.BeginOutputReadLine();
var myWriter = p.StandardInput;
myWriter.AutoFlush = true;
myWriter.Close();
p.WaitForExit();
var output = outputStringBuilder.ToString();
return output;
}
Problem2 Method is success read output, I want read output Problem1 method.
I believe I am on the right track but just need a couple pointers.
Test project on the github
Something like this:
private string ReadProcessOutput(string fileName, TimeSpan waitTime, string args, string commandToEnter) // Command to enter in input window.
{
Console.WriteLine("Starting process: {0}", fileName);
Process proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
CreateNoWindow = true
}
};
proc.Start();
proc.StandardInput.WriteLine(commandToEnter);
proc.WaitForExit((int)waitTime.TotalMilliseconds);
if (proc.HasExited)
{
Console.WriteLine("Process {0} exited with code {1}", fileName, proc.ExitCode);
string output = proc.StandardOutput.ReadToEnd();
Console.WriteLine("Process output: " + Environment.NewLine + output);
return output;
}
return null;
}
I am running an exe through commandline and getting following output.
C:\Users\sysadmin>C:\Users\sysadmin\Desktop\New_folder\Setup\PatchInstaller.exe
--mode=silent
C:\Users\sysadmin Begin Setup UI mode: Silent Error :
Another instance running, Only a single instance can be run at a time.
Exit Code: 11
i am running this through System.daignostics.process.
My issue is PatchInstaller.exe calling another process and the output of that nested process is what is visible with cmd. but the same result and exit code i am not able to get through Process object of PatchInstaller.exe.
Is there any way of getting output of process running within process?
Following is the code i have tired...
string command = #"C:\Users\sysadmin\Desktop\Setup\PatchInstaller.exe";
string result = string.Empty;
System.Diagnostics.ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command + " --mode=silent);
System.Diagnostics.Process proc = new Process();
procStartInfo.ErrorDialog = false;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
// Do not create the black window.
procStartInfo.CreateNoWindow = true;
if (!string.IsNullOrEmpty(domain) && !string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pwd))
{
procStartInfo.Domain = domain;
procStartInfo.UserName = user;
System.Security.SecureString ss = new System.Security.SecureString();
foreach (char c in pwd) { ss.AppendChar(c); }
procStartInfo.Password = ss;
}
proc = System.Diagnostics.Process.Start(procStartInfo);
proc.ErrorDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs errorLine)
{
if (errorLine.Data != null) result += "error:" + errorLine.Data +;
};
proc.OutputDataReceived += delegate(object sender, System.Diagnostics.DataReceivedEventArgs outputLine)
{
if (outputLine.Data != null) result += outputLine.Data +;
};
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
Process[] pname = Process.GetProcessesByName("PatchInstaller");
Process[] processlist = Process.GetProcesses();
foreach (Process theprocess in processlist)
{
Console.WriteLine("Process: {0} ID: {1}", theprocess.ProcessName, theprocess.Id);
}
proc.WaitForExit();
I don't know much about ProcessStartInfo but I have used Process before and the way to get the information out of the standard output is shown as below, I assume it should be a similar way just by accessing the StandardOutput
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = false;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine(command);
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
var output = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
This code worked for me:
const int MAX_EXIT_WAIT_TIME = 3000;
// Fill needed data
string username = "";
string password = "";
string domain = "";
string appName = "";
var dir = Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName);
var appFullPath = Path.Combine(dir, appName);
ProcessStartInfo psi = new ProcessStartInfo(appFullPath);
psi.UserName = username;
var securePass = new System.Security.SecureString();
foreach (var c in password)
securePass.AppendChar(c);
psi.Password = securePass;
psi.Domain = domain;
psi.LoadUserProfile = false;
psi.WorkingDirectory = dir;
psi.Arguments = "";
psi.RedirectStandardOutput = true;
// Create Process object, but not start it!
var proc = new Process();
proc.StartInfo = psi;
StringCollection values = new StringCollection();
DataReceivedEventHandler outputDataReceived = (o, e) =>
{
lock (values)
values.Add(e.Data);
};
try
{
proc.OutputDataReceived += outputDataReceived;
// Only here we start process
if (!proc.Start())
throw new InvalidOperationException("Couldn't start app");
proc.BeginOutputReadLine();
proc.WaitForExit(MAX_EXIT_WAIT_TIME);
}
finally { proc.OutputDataReceived -= outputDataReceived; }
Console.WriteLine("Read {0} ", values.Count);
foreach (var item in values)
Console.WriteLine(" {0}", item);
I'm calling Rscript.exe from c# windows forms app, and the cmd window opens up for a few milliseconds. The python solution was easy: simply started pythonw.exe instead of python.exe
Here is the function:
private void runr()
{
orig = richTextBox1.Text;
System.IO.File.WriteAllText(#"C:\cp.R", richTextBox1.Text);
string cmd = #"C:\cp.R";
string args = #"";
ProcessStartInfo start = new ProcessStartInfo();
start.FileName = #"C:\Program Files\R\R-3.2.3\bin\Rscript.exe";
start.Arguments = string.Format("{0} {1}", cmd, args);
start.UseShellExecute = false;
start.RedirectStandardOutput = true;
start.RedirectStandardError = true;
using (Process process = Process.Start(start))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
StreamReader reader2 = process.StandardError;
string err = reader2.ReadToEnd();
richTextBox1.Text = result;
if (!string.IsNullOrEmpty(err))
richTextBox1.Text += "\nError:\n" + err;
}
}
}
Setting start.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; does not work.
Set the ProcessStartInfo.CreateNoWindow Property to true. This prevents the system from creating a console window for the new process.
I have a netsh profiler that I want to get detailed information for each loop found.
private void wifiButton_Click(object sender, EventArgs e)
{
Process cmd = new Process();
cmd.StartInfo.FileName = "netsh.exe";
System.Threading.Thread.Sleep(50);
cmd.StartInfo.Arguments = "wlan show profiles";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
//* Read the output (or the error)
string output = cmd.StandardOutput.ReadToEnd();
textBox3.Text = output;
cmd.WaitForExit();
// output would be set by earlier code
var Output = textBox3.Text;
var regex = new Regex(#"All User Profile[\s]+: (.*)");
var resultList = new List<string>();
foreach (Match match in regex.Matches(Output))
{
resultList.Add(match.Groups[1].ToString());
}
textBox4.Text = string.Join(", ", resultList);
The code above works, below I want to take the list and run the results through CMD again to get the detailed results.
for (int i = 0; i < resultList.Count; i++)
{
Process cmd2 = new Process();
cmd.StartInfo.FileName = "netsh.exe";
System.Threading.Thread.Sleep(50);
cmd.StartInfo.Arguments = "wlan show profiles name=" resultList(0) + " key=clear";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
//* Read the output (or the error)
string output2 = cmd2.StandardOutput.ReadToEnd();
textBox5.Text = output2;
cmd.WaitForExit();
}
}
Im a good and used a 0 instead of the i placeholder. Also, mislabeled my second cmd run and forgot to update to cmd2, therefore the previous command line would overlap itself and break.
private void wifiButton_Click(object sender, EventArgs e)
{
Process cmd = new Process();
cmd.StartInfo.FileName = "netsh.exe";
System.Threading.Thread.Sleep(50);
cmd.StartInfo.Arguments = "wlan show profiles";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.RedirectStandardError = true;
cmd.Start();
//* Read the output (or the error)
string output = cmd.StandardOutput.ReadToEnd();
textBox3.Text = output;
cmd.WaitForExit();
System.Threading.Thread.Sleep(500);
// output would be set by earlier code
var Output = textBox3.Text;
var regex = new Regex(#"All User Profile[\s]+: (.*)");
var resultList = new List<string>();
foreach (Match match in regex.Matches(Output))
{
resultList.Add(match.Groups[1].ToString());
}
textBox4.Text = string.Join(", ", resultList);
System.Threading.Thread.Sleep(500);
for (int i = 0; i < resultList.Count; i++)
{
Process cmd2 = new Process();
cmd2.StartInfo.FileName = "netsh.exe";
System.Threading.Thread.Sleep(50);
cmd2.StartInfo.Arguments = "wlan show profiles name=" + resultList[i] + " key=clear";
cmd2.StartInfo.UseShellExecute = false;
cmd2.StartInfo.RedirectStandardOutput = true;
cmd2.StartInfo.RedirectStandardError = true;
cmd2.Start();
//* Read the output (or the error)
string output2 = cmd2.StandardOutput.ReadToEnd();
textBox5.Text = output2;
cmd2.WaitForExit();
}
}