C# 코드에서 WMI로 스크립트 (VBS)를 시작하는 방법은 무엇입니까?

StackOverflow https://stackoverflow.com/questions/925999

  •  06-09-2019
  •  | 
  •  

문제

AC# 코드에서 WMI를 사용하여 Visual Basic 스크립트 파일을 시작해야합니다.

이 코드에 무엇이 잘못되었는지 잘 모르겠습니다. 결과는 항상 8 (알 수없는 실패)입니다. 그러나 예를 들어 Notepad.exe는 실패없이 시작할 수 있습니다.

        //Get the object on which the method will be invoked
        ManagementClass processClass = new ManagementClass("Win32_Process");

        //Create an array containing all arguments for the method
        object[] methodArgs = { "C:\\MyFolder\\Test.vbs arg1 arg2", null, null, 0 };

        //Execute the method
        object result = processClass.InvokeMethod("Create", methodArgs);
도움이 되었습니까?

해결책

스크립트는 실행 파일이 아닙니다. Windows 스크립트 호스트에서 실행되므로 스크립트 이름 앞에 cscript.exe 또는 wscript.exe 로의 경로를 지정해야합니다.

object[] methodArgs = {   
                        Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cscript.exe") + @" C:\MyFolder\Test.vbs arg1 arg2",
                        null,
                        null,
                        0
                      };

다른 팁

이런 유형의 물건에 대해 많이 알지 못하지만 스크립트 호스트를 호출하여 VBS 파일을 전달해야 할 수도 있다고 생각합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top