【发布时间】:2015-06-21 00:56:29
【问题描述】:
大家好,我已经创建这个程序一天了。但似乎我无法让它工作。我想要的是,我有一个 Windows 窗体,它将获取 IP 地址输入并执行一个 .bat 文件。
@echo off
wmic /node:x computersystem get username
其中 x 是我的 IP 的字符串变量。 该命令将打印出当前登录的用户名。
这是我的 C# 代码:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace wmic_forms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string x;
private void button1_Click(object sender, EventArgs e)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "C:\\ken.bat";
Process p = Process.Start(psi);
string strOutput = p.StandardOutput.ReadToEnd();
//p.WaitForExit();
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.Arguments = textBox1.Text;
MessageBox.Show(strOutput);
//Console.WriteLine(strOutput);
//Console.ReadLine();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//my input text box for IP
x = textBox1.Text;
}
}
}
代码运行没有错误。但它只会从 .bat 文件中打印出整个字符串。请给我反馈,告诉我如何将变量 x 传递给 .bat 文件并执行它的命令。
【问题讨论】:
标签: c# windows batch-file