2014-06-09 2 views
-1

원격 서버에서 캐시를 지우는 간단한 배치 파일을 작성했습니다. 나는 사용자 이름과 서버 위치로 원격 서버에 로그인하여 작업을 수행 할 수 있기를 원합니다. 그래서 나는이 작업을 수행 할 수있는 winform을 개발했습니다. 즉, 버튼 클릭과 배치 파일 실행입니다. 서버와의 연결이없는 경우 배치 파일 (RGB_CLEAR_APPV_LCACHE.002.bat >> AppVCache1_ % date :/= % .log)에 포함 된 로그 파일에보고하십시오.winform 컨트롤에서 배치 파일을 실행하는 방법

내 문제는 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 
+0

명령 줄 실행 파일에 인수를 전달하는 방법을 묻습니다. – 48klocs

+0

아마 그 질문 일 수도 있습니다. 그러나 사용자가 서버의 이름과 위치를 입력 할 때 명확한 용어로 배치 명령을 실행해야합니다. 실패하면 txt 보고서로보고해야합니다. – user3722925

답변

2

당신은 인수 속성, 즉를 사용하여 배치 파일로 전달할 수 있습니다

proc.StartInfo.Arguments = userNameTexBox.Text + " " + userLocationTextbox.Text 

.. . 그리고 나서, b 안에서 atch 파일에서 다음과 같은 매개 변수를 읽습니다.

set USERNAME_IS=%1 
set LOC=%2 
+0

코드가 조언에 감사 드리며 감사드립니다. 내가 직면 한 문제는 배치 파일을 실행 한 후 로그 보고서를 출력하는 방법입니다. RGB_CLEAR_APPV_LCACHE.002.bat >> AppVCache1_ % date :/= %. log를 사용했지만 솔루션 파일 폴더에 출력을 생성하지 않습니다. 디버그 폴더에서 이벤트 로그를 실행하지만 완전히 비어 있습니다. 하지만 파일 폴더에서 더블 클릭하면 같은 폴더에 로그 파일이 생성됩니다. 버튼 클릭 이벤트가 발생하면 솔루션 파일에서 로그 보고서를 생성합니다. 또는 일괄 처리에서 로그 파일을 가져 오는 더 좋은 방법이 있습니까? – user3722925

관련 문제