我有使用 ProcessStartInfo 和 Process 来调用另一个脚本并返回该脚本输出的代码。
不幸的是,我遇到了错误,我不确定如何解决它们。
#script1.ps1
$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL
<#Previously created password file in C:\Script\cred.txt, read-host -assecurestring | convertfrom-securestring | out-file C:\Script\cred.txt#>
$password = get-content C:\Script\cred.txt | convertto-securestring
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = "powershell.exe"
$startInfo.Arguments = "C:\script\script2.ps1", $abc
$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Username = "DOMAIN\Username"
$startInfo.Password = $password
$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$standardOut = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()
# $standardOut …Run Code Online (Sandbox Code Playgroud) 我正在尝试从 Azure webjob 运行 python 脚本。这是我按照此链接所做的
https://<webapp name>.scm.azurewebsites.net并Python 364x86通过站点扩展选项卡安装Python 364x86安装在以下路径:D:\home\python364x86trading.py中D:\home\python364x86run.bat用这行代码创建文件D:\home\python364x86\python.exe trading.pyrun.bat和trading.py在webjob zip文件[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Initializing
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Run script 'run.bat' with script host - 'WindowsScriptHost'
[09/07/2019 07:02:00 > 0dd02c: SYS INFO] Status changed to Running
[09/07/2019 07:02:00 > 0dd02c: ERR ] The …Run Code Online (Sandbox Code Playgroud) 我正在使用该Process.Start()方法从我的.NET代码启动一个控制台应用程序的新实例.我想知道是否可以指定托管生成进程的控制台窗口的标题.找不到合适的东西ProcessStartInfo.
作为最后的手段,我可以P/Invoke直接与Win32 API交谈,但我宁愿不这样做.
有任何想法吗?
谢谢.
我怀疑这可能适用于多种编程语言,但在这种情况下,我指的是.NET.
当我使用时System.Diagnostics.Process.Start,我可以包括一个System.Diagnostics.ProcessStartInfo对象作为参数.ProcessStartInfo类的一个属性是Verb(字符串的类型).还有一个string []属性,Verbs其中似乎包含允许值列表Verb.
我注意到在Verbs数组中有一个"Print"值和一个"PrintTo"值.这两者有什么区别?我测试了两者,它们似乎都导致文件打印到我的默认打印机.
我在Debian中有一个需要root特权的程序,而myuser必须运行它,但是我必须从以单声道运行的.NET应用程序(C#)进行调用。在/ etc / sudoers中,添加以下行:
myuser ALL = NOPASSWD: /myprogram
Run Code Online (Sandbox Code Playgroud)
因此sudo ./myprogram适用于myuser。
在。我在代码中使用的.NET
string fileName = "/myprogram";
ProcessStartInfo info = new ProcessStartInfo (fileName);
...
Run Code Online (Sandbox Code Playgroud)
如何调用“ sudo fileName”?到那时还行不通...谢谢你,莫妮克。
我需要从我的 UWP 应用程序调用批处理文件。这样做的方法似乎是Process.Start(),但它说当我按照它输出的路径时,它甚至没有找到文件,它肯定存在。文件路径和工作目录都作为使用 shellexecute = false 时所需的完整路径给出。
当我设置 useshellexecute = true 时它起作用。由于完整路径在这里有效,文件显然就在那里。使用 shellexecute = true 时,工作目录仅指定应搜索文件的位置,命令提示符在 system32 目录中启动,但我需要工作目录是打开的批处理所在的位置。
因此,ShellExecute = false。
我试过: 1. ShellExecute = true。它找到了文件,但工作目录设置不正确。2. 硬编码批处理的绝对路径。还是没找到。3. 设置 StartInfo.FileName 而不是通过参数给它。4. 相对路径 5. Process.Start(Filename)。Can't set Working Directory without StartInfo 6.看类似的问题,但答案总是我已经有了(当shellexecute = false时使用完整路径)
string executable = args[2];
string path = Assembly.GetExecutingAssembly().CodeBase;
string directory = Path.GetDirectoryName(path);
var startInfo = new ProcessStartInfo(directory + @"\Diagnose\_data\Updater\" + executable);
startInfo.UseShellExecute = false;
startInfo.WorkingDirectory = directory + @"\Diagnose\_data\Updater";
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
Process.Start(startInfo); …Run Code Online (Sandbox Code Playgroud) 我试图让以下代码工作,所以我可以从我的c#程序调用perl脚本.我正在使用xd service pack3上的visual stdio 2008进行开发.
myProcess = new Process();
ProcessStartInfo myProcessStartInfo = new ProcessStartInfo("perl.exe");
myProcessStartInfo.Arguments = @"C:\Documents and Settings\test_perl.pl";
myProcessStartInfo.UseShellExecute = false;
myProcessStartInfo.RedirectStandardOutput = true;
myProcessStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
myProcessStartInfo.CreateNoWindow = true;
myProcess.StartInfo = myProcessStartInfo;
myProcess.Start();
string output = myProcess.StandardOutput.ReadToEnd();
MessageBox.Show(output);
myProcess.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
我验证test_perl.pl存在,如果我将perl.exe更改为notepad.exe,则上述代码可以正常工作.但是,如果我使用perl.exe,则消息框为空.
无法弄清楚为什么这是错的.如果你知道原因,请帮助我.
谢谢
我有这个小功能,可以省去处理可怕的System.Diagnostics.Process API的一些麻烦:
let HiddenExec (command: string, arguments: string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(command)
startInfo.Arguments <- arguments
startInfo.UseShellExecute <- false
startInfo.RedirectStandardError <- true
startInfo.RedirectStandardOutput <- true
use proc = System.Diagnostics.Process.Start(startInfo)
proc.WaitForExit()
(proc.ExitCode,proc.StandardOutput.ReadToEnd(),proc.StandardError.ReadToEnd())
Run Code Online (Sandbox Code Playgroud)
这很好用,因为我得到了一个包含exitcode,stdout和stderr结果的三个元素的元组.
现在,假设我不想"隐藏"执行.也就是说,我想写一个假设的,更简单的Exec函数.然后解决方案是不重定向stdout/stderr,我们完成了:
let Exec (command: string, arguments: string) =
let startInfo = new System.Diagnostics.ProcessStartInfo(command)
startInfo.Arguments <- arguments
startInfo.UseShellExecute <- false
let proc = System.Diagnostics.Process.Start(startInfo)
proc.WaitForExit()
proc.ExitCode
Run Code Online (Sandbox Code Playgroud)
但是,如果我可以重构这两个函数将它们聚合成一个函数,并且只是向它传递一个"隐藏的"bool标志,那将是很好的:
let NewExec (command: string, arguments: string, hidden: bool) =
Run Code Online (Sandbox Code Playgroud)
这样,NewExec(_,_,false)也会返回stdout,stderr(不仅像之前一样退出exitCode).问题是,如果我不进行重定向舞蹈(startInfo.RedirectStandardError <- true),那么我以后无法从输出中读取,proc.StandardOutput.ReadToEnd()因为我得到了错误StandardOut has not …
.net f# system.diagnostics processstartinfo redirectstandardoutput
我想创建一个进程,使用shutdown.exe在给定时间后关闭计算机.
这是我的代码:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "–s –f –t " + seconds;
Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)
其中seconds是int局部变量,用户决定.
当我运行我的代码时没有任何反应.但是当我手动进入cmd提示符并键入:
shutdown.exe - s -f -t 999时,
Windows将弹出一个弹出窗口并告诉我系统将在16分钟内关闭.
我认为这是因为多个参数的原因是我的方法中止正在进行的系统关闭工作(我从cmd提示符手动创建了systemshutdown).这几乎是相同的,除了在startInfo.Argument:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.FileName = "shutdown.exe";
startInfo.Arguments = "-a";
Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud) 我有一个.Net应用程序,它接受一堆命令行参数,处理其中一些,并将其余部分用作另一个应用程序的参数
例如
MyApp.exe foo1 App2.exe arg1 arg2 ...
Run Code Online (Sandbox Code Playgroud)
MyApp.exe是我的应用程序,
foo1是我的应用程序关心的参数.App2.exe是另一个应用程序,我的应用程序将运行带有arg1 arg2等的App2作为参数.
目前我的应用程序只是使用这样的东西运行App2.exe
Process.Start(args[1], String.Join(" ", args.Skip(2)).因此上面的命令将正确运行:带有参数"arg1 arg2"的App2.exe.但是,请考虑这样的事情
MyApp.exe foo1 notepad.exe "C:\Program Files\readme.txt"
Run Code Online (Sandbox Code Playgroud)
上面的代码不会知道引号,并将使用参数C:\ Program Files\readme.txt(不带引号)运行notepad.exe.我该如何解决这个问题?
.net c# processstartinfo console-application command-line-arguments