如何从winform控件启动批处理文件
本文关键字:启动 批处理文件 控件 winform | 更新日期: 2023-09-27 18:07:37
我编写了一个简单的批处理文件来清除远程服务器中的缓存。我希望能够通过登录到远程服务器的用户名和服务器位置执行任务。所以我开发了一个winform,使我能够执行这个任务,即按一下按钮,运行批处理文件。如果与服务器没有连接,则报告到批处理文件(rgb_clear_appv_lache .002.bat>> AppVCache1_%date:/=%.log)
中的日志文件。我的问题是,我不能传递用户名和位置从winform到批处理文件。我已经尽力了,但无济于事。下面是c#代码和批处理文件代码。我不确定问题是来自批处理文件还是c#代码。
任何有助于使代码工作的替代方案或建议都将受到赞赏。
c#代码public partial class AppVForm : Form
{
public AppVForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var file2 = Path.Combine(Environment.CurrentDirectory, @"Files'cleaner2.bat");
var file = Path.Combine(Environment.CurrentDirectory, @"Files'RGB_CLEAR_APPV_LCACHE.002.bat");
string text = File.ReadAllText(file);
text = text.Replace("##USERNAME##", userNameTexBox.Text);
text = text.Replace("##LOCATION##", userLocationTextbox.Text);
File.WriteAllText(file2, text);
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = file2;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.WaitForExit();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void AppVForm_Load(object sender, EventArgs e)
{
}
private void userNameTexBox_Validating(object sender, CancelEventArgs e)
{
bool cancel = false;
if (string.IsNullOrEmpty(this.userNameTexBox.Text))
{
cancel = true;
errorProvider1.SetError(this.userNameTexBox, "You must provide a username.");
}
e.Cancel = cancel;
}
private void userNameTexBox_Validated(object sender, EventArgs e)
{
this.errorProvider1.SetError(userNameTexBox, string.Empty);
}
private void userLocationTextbox_Validating(object sender, CancelEventArgs e)
{
bool cancel = false;
if (string.IsNullOrEmpty(this.userLocationTextbox.Text))
{
cancel = true;
errorProvider1.SetError(this.userLocationTextbox, "You must provide a valid location.");
}
e.Cancel = cancel;
}
private void userLocationTextbox_Validated(object sender, EventArgs e)
{
this.errorProvider1.SetError(userLocationTextbox, string.Empty);
}
}
批处理文件代码
::---------------------DECLARE STUFF HERE---------------------------
::---- Leave Colons and speech marks in place, the guts get messy without them!!--------
::---- LOC is the location of the machines C drive... If over network, use "''COMPNAME'c$' - With speech mark. If locally, use "C:' - With Speech Mark----------
set USERNAME_IS=##USERNAME##
set LOC="##LOCATION##
set H_1="''greenwich'users'Home'
set h_2='AppData'Roaming'SoftGrid Client'
set c_1=%##LOCATION##%users'
set c_2='appdata'local'softgrid client'*.*?"
SET "STR1=%H_1%%##USERNAME##%%H_2%"
SET "STR2=%c_1%%##USERNAME##%%c_2%"
::---------------------Remove Files from H DRIVE ---------------------
move /y %STR1%shortcut_ex.dat %STR1%Icon Cache"
move /y ::%STR1%userinfo.dat" %STR1%Icon Cache"
ATTRIB +H %STR1%Icon Cache"
FOR /D %%i IN (%STR1%*") DO RD /S /Q "%%i" DEL /Q %STR1%*.*"
ATTRIB -H %STR1%Icon Cache"
move /y %STR1%Icon Cache'shortcut_ex.dat" %STR1%"
move /y %STR1%Icon Cache'userinfor.dat" %STR1%
::---------------------Remove Files from C DRIVE APPFS---------------------
del %##LOCATION##%ProgramData'Microsoft'Application Virtualization Client'SoftGrid Client'AppFS Storage'*.*?"
::---------------------:Remove Files from C Drive SOFTGRID---------------------
del %STR2%
@IF ERRORLEVEL 1 GOTO failLabel
:successLabel
ECHO Success
GOTO endLabel
:failLabel
@ECHO Clean up Failed
:endLabel
pause
RGB_CLEAR_APPV_LCACHE.002.bat >> AppVCache1_%date:/=%.log
pause
您可以使用arguments属性将它们传递给批处理文件,例如:
proc.StartInfo.Arguments = userNameTexBox.Text + " " + userLocationTextbox.Text
…然后,在批处理文件中,像这样读取参数:
set USERNAME_IS=%1
set LOC=%2