无法将字符串转换为char [英] Cannot convert string to char

查看:101
本文介绍了无法将字符串转换为char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我创建了一个运行命令提示符命令的winform应用程序,但问题是我收到错误如无法将字符串转换为字符串这一行psi = new ProcessStartInfo(TextBox1 .Text.Split()(0),TextBox1.Text.Split()(1));任何人都可以帮助我

我也试过
psi = new ProcessStartInfo(TextBox1.Text.Split('')(0),TextBox1.Text.Split('')(1));
然后我收到错误,如方法名称预期

请帮助解决此错误以及如何在该拆分中添加空格。





我尝试过:



使用系统; 
使用System.Collections;
使用System.Collections.Generic;
使用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。拆分()(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();
}
}

解决方案

string.Split()返回一个数组,你应该这样做:

  var  arr = TextBox1.Text.Split(< span class =code-string>' '); 
psi = new ProcessStartInfo(arr [ 0 ],arr [ 1 ]);


您需要传递char数组或字符串数​​组以进行拆分,并使用索引访问数组方括号而不是弯曲的。



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


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();
}
}

解决方案

string.Split() returns an array so you should do:

var arr = TextBox1.Text.Split(' ');
psi = new ProcessStartInfo(arr[0], arr[1]);


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]


这篇关于无法将字符串转换为char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆