Click here to Skip to main content
14,837,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everybody, i have created a winform application which run Command Prompt Commands but the problems is  i am getting an error Like "Cannot Convert string to char" in this line " psi = new ProcessStartInfo(TextBox1.Text.Split(" ")(0), TextBox1.Text.Split(" ")(1)); "  Can anybody please help me
 
I also tried 
psi = new ProcessStartInfo(TextBox1.Text.Split(' ')(0), TextBox1.Text.Split(' ')(1));
then i am getting error like Method Name Expected

Please help with this error and how add whitespace in that split.


What I have tried:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
public partial class Form1:Form
{
private ProcessStartInfo psi;
private Process cmd;
private delegate void InvokeWithString(string text);
private void Button1_Click(System.Object sender, System.EventArgs e)
{
try {
cmd.Kill();
} catch (Exception ex) {
}
TextBox2.Clear();
if (TextBox1.Text.Contains(" ")) {
psi = new ProcessStartInfo(TextBox1.Text.Split(" ")(0), TextBox1.Text.Split(" ")(1));
} else {
psi = new ProcessStartInfo(TextBox1.Text);
}
System.Text.Encoding systemencoding = default(System.Text.Encoding);
System.Text.Encoding.GetEncoding(Globalization.CultureInfo.CurrentUICulture.TextInfo.OEMCodePage);
var _with1 = psi;
_with1.UseShellExecute = false;
_with1.RedirectStandardError = true;
_with1.RedirectStandardOutput = true;
_with1.RedirectStandardInput = true;
_with1.CreateNoWindow = true;
_with1.StandardOutputEncoding = systemencoding;
_with1.StandardErrorEncoding = systemencoding;
cmd = new Process {
StartInfo = psi,
EnableRaisingEvents = true
};
cmd.ErrorDataReceived += Async_Data_Received;
cmd.OutputDataReceived += Async_Data_Received;
cmd.Start();
cmd.BeginOutputReadLine();
cmd.BeginErrorReadLine();
}
private void Async_Data_Received(object sender, DataReceivedEventArgs e)
{
this.Invoke(new InvokeWithString(Sync_Output), e.Data);
}
private void Sync_Output(string text)
{
TextBox2.AppendText(text + Environment.NewLine);
TextBox2.ScrollToCaret();
}
}
Posted
Updated 20-Nov-17 23:41pm

string.Split() returns an array so you should do:
C#
var arr = TextBox1.Text.Split(' ');
psi = new ProcessStartInfo(arr[0], arr[1]);
   
Comments
Member 12414304 21-Nov-17 5:37am
   
Hi Mehdi Gholam,
Thank you , it was working.
You need to pass a char array or string array to split, and you access arrays by index using square brackets rather than curved ones.

TextBox1.Text.Split(new char[]{' '})[0]
   
Comments
Member 12414304 21-Nov-17 5:43am
   
No it wont work , if you try this code you will get error like " Method Name Expected "
F-ES Sitecore 21-Nov-17 5:46am
   
You didn't even try it. You're getting that error because you're doing this

TextBox1.Text.Split(new char[]{' '})(0)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900