我在项目资源中添加了 script3.ps1 文件。 我构建了 dll 文件。
我想用 script.ps1 启动一个 powershell 进程。 我试过了
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = "-ExecutionPolicy ByPass -Command \".'"+MyProject.Properties.Resources.script3+"'",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
我收到错误:
the term 'system.byte[]' is not recognized as the name of a cmdlet,
function, script file or operable program. check the spelling of the name, or if
a path was included, verify that path is correct and try again. at line : 1 char
最佳答案
先将资源内容保存到文件中:
File.WriteAllBytes("file3.ps", MyProject.Properties.Resources.script3);
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = "-ExecutionPolicy ByPass -Command \".\\file3.ps\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
或者如果它的一行只是转换为字符串:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "powershell.exe",
Arguments = "-ExecutionPolicy ByPass -Command \""+Encoding.Default.GetString(MyProject.Properties.Resources.script3)+"\"",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
关于c# - 如何在 C# 项目中添加 script.ps1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31723685/