c#在文本框中删除多行并将它们分开

时间:2012-08-18 02:09:19

标签: c#

您好我正在尝试编写一个需要3个用户输入的程序:文件路径,用户名和计算机名,然后为列表中的每个计算机名创建一个批处理文件。我还没有编写创建批处理文件部分,但是我无法从计算机文本框中取出多台计算机并从中创建单独的代码行。

因此,如果计算机文本框中有3个计算机名称,id就像当用户点击按钮时,输出每个计算机名称在另一行上。

如果计算机名称文本框包含以下计算机名称:M22-LIBRL74258S,M22-LIBRL74257S和M22-LIBRL74256S

输出将是:

XCOPY“C:\ Documents and Settings \ Administrator \ Desktop \ file.exe”“\ M22-LIBRL74258S \ c $ \ Users \ username \ desktop”

XCOPY“C:\ Documents and Settings \ Administrator \ Desktop \ file.exe”“\ M22-LIBRL74257S \ c $ \ Users \ username \ desktop”

XCOPY“C:\ Documents and Settings \ Administrator \ Desktop \ file.exe”“\ M22-LIBRL74256S \ c $ \ Users \ username \ desktop”

谢谢!

WindowsFormsApplication1
{
       public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void browsebtn_Click(object sender, EventArgs e)
    {
        {

            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title = "Select a File to Send";

            // Show the Dialog.
            // If the user clicked OK in the dialog and

            if (openFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                string strfilename = openFileDialog1.InitialDirectory + openFileDialog1.FileName;
                pathtxt.Text = strfilename.ToString();
            }

        }
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void createbtn_Click(object sender, EventArgs e)
    {

        string filepath = pathtxt.Text.ToString();

        string username = usertxtbox.Text.ToString();




        string computer = computerstxtbox.Text;
        string[] split = computer.Split(new char[] { '\n' });



        foreach (string l in split)
        {

            var strIP = "XCOPY \"" + pathtxt.Text + '"' + ' ' + '"' + "\\" + "\\" + l + "\\" + "c$" + "\\" + "Users" + "\\" + username + "\\" + "desktop" + '"';
            //This is to see it

            MessageBox.Show(strIP);
        }

1 个答案:

答案 0 :(得分:0)

尝试并替换

string[] split = computer.Split(new char[] { '\n' });

代替

string[] split = computer.Split(new String[] {"\r\n"}, StringSplitOptions.None);

此外,如果您的计算机名称以逗号分隔,则还必须在拆分中指定它们。例如new String [] {“\ r \ n”,“,”}