我写了一个简单的批处理文件来清除远程服务器中的缓存。我希望能够通过使用用户名和服务器位置登录到远程服务器来执行任务。所以我开发了一个winform,使我能够完成这项任务,即点击按钮,运行批处理文件。如果没有与服务器的连接,请将其报告给批处理文件中包含的日志文件(RGB_CLEAR_APPV_LCACHE.002.bat>> AppVCache1_%date:/ =%。log)
我的问题是,我无法将用户名和位置从winform传递到批处理文件。我尽力而为但无济于事。 c#代码和批处理文件代码如下。我不确定问题是来自批处理文件还是c#代码。
任何有助于使代码生效的替代和建议都将受到赞赏。
c#code
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
答案 0 :(得分:2)
您可以使用arguments属性将它们传递给批处理文件,即:
proc.StartInfo.Arguments = userNameTexBox.Text + " " + userLocationTextbox.Text
...然后,在批处理文件中,读入如下参数:
set USERNAME_IS=%1
set LOC=%2