有没有办法在c#应用程序中运行命令提示符命令?如果是这样,我该如何做以下几点:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是在JPG图像中嵌入一个RAR文件。我只是想知道在c#中是否有一种方法可以自动做到这一点。
有没有办法在c#应用程序中运行命令提示符命令?如果是这样,我该如何做以下几点:
copy /b Image1.jpg + Archive.rar Image2.jpg
这基本上是在JPG图像中嵌入一个RAR文件。我只是想知道在c#中是否有一种方法可以自动做到这一点。
当前回答
这可能有点读所以我提前抱歉。这是我尝试和测试过的方法,可能有更简单的方法,但这是我把代码扔到墙上,看看什么卡住了
如果它可以用批处理文件完成,那么可能过于复杂的工作是用c#编写一个。bat文件并运行它。如果你想要用户输入,你可以把输入放到一个变量中,然后用c#把它写进文件中。这种方法需要反复试验,因为它就像用另一个木偶控制一个木偶。
这里是一个例子,在这种情况下,该功能是一个按钮在windows论坛应用程序,清除打印队列。
using System.IO;
using System;
public static void ClearPrintQueue()
{
//this is the path the document or in our case batch file will be placed
string docPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//this is the path process.start usues
string path1 = docPath + "\\Test.bat";
// these are the batch commands
// remember its "", the comma separates the lines
string[] lines =
{
"@echo off",
"net stop spooler",
"del %systemroot%\\System32\\spool\\Printers\\* /Q",
"net start spooler",
//this deletes the file
"del \"%~f0\"" //do not put a comma on the last line
};
//this writes the string to the file
using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
{
//This writes the file line by line
foreach (string line in lines)
outputFile.WriteLine(line);
}
System.Diagnostics.Process.Start(path1);
}
如果你想要用户输入,那么你可以尝试这样做。
这是为了将计算机IP设置为静态,但询问用户IP,网关和dns服务器是什么。
你需要这个才能让它工作
public static void SetIPStatic()
{
//These open pop up boxes which ask for user input
string STATIC = Microsoft.VisualBasic.Interaction.InputBox("Whats the static IP?", "", "", 100, 100);
string SUBNET = Microsoft.VisualBasic.Interaction.InputBox("Whats the Subnet?(Press enter for default)", "255.255.255.0", "", 100, 100);
string DEFAULTGATEWAY = Microsoft.VisualBasic.Interaction.InputBox("Whats the Default gateway?", "", "", 100, 100);
string DNS = Microsoft.VisualBasic.Interaction.InputBox("Whats the DNS server IP?(Input required, 8.8.4.4 has already been set as secondary)", "", "", 100, 100);
//this is the path the document or in our case batch file will be placed
string docPath =
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
//this is the path process.start usues
string path1 = docPath + "\\Test.bat";
// these are the batch commands
// remember its "", the comma separates the lines
string[] lines =
{
"SETLOCAL EnableDelayedExpansion",
"SET adapterName=",
"FOR /F \"tokens=* delims=:\" %%a IN ('IPCONFIG ^| FIND /I \"ETHERNET ADAPTER\"') DO (",
"SET adapterName=%%a",
"REM Removes \"Ethernet adapter\" from the front of the adapter name",
"SET adapterName=!adapterName:~17!",
"REM Removes the colon from the end of the adapter name",
"SET adapterName=!adapterName:~0,-1!",
//the variables that were set before are used here
"netsh interface ipv4 set address name=\"!adapterName!\" static " + STATIC + " " + STATIC + " " + DEFAULTGATEWAY,
"netsh interface ipv4 set dns name=\"!adapterName!\" static " + DNS + " primary",
"netsh interface ipv4 add dns name=\"!adapterName!\" 8.8.4.4 index=2",
")",
"ipconfig /flushdns",
"ipconfig /registerdns",
":EOF",
"DEL \"%~f0\"",
""
};
//this writes the string to the file
using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
{
//This writes the file line by line
foreach (string line in lines)
outputFile.WriteLine(line);
}
System.Diagnostics.Process.Start(path1);
}
就像我说的。它可能有点过于复杂,但它从来没有失败,除非我写错批处理命令。
其他回答
出于某种原因,上面的答案都没有帮助,似乎他们把错误扫到地毯下,使故障排除变得困难。所以我最终选择了这样的东西,也许它会帮助到其他人:
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = @"C:\Program Files\Microsoft Visual Studio 14.0\Common7\IDE\tf.exe",
Arguments = "checkout AndroidManifest.xml",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true,
WorkingDirectory = @"C:\MyAndroidApp\"
}
};
proc.Start();
是的,有(参见Matt Hamilton的评论中的链接),但是使用。net的IO类会更简单更好。您可以使用File。ReadAllBytes读取文件,然后是File。写“嵌入式”版本。
你可以在一行中使用CliWrap来做到这一点:
var result = await Cli.Wrap("cmd")
.WithArguments("copy /b Image1.jpg + Archive.rar Image2.jpg")
.ExecuteBufferedAsync();
var stdOut = result.StandardOutput;
var proc1 = new ProcessStartInfo();
string anyCommand;
proc1.UseShellExecute = true;
proc1.WorkingDirectory = @"C:\Windows\System32";
proc1.FileName = @"C:\Windows\System32\cmd.exe";
proc1.Verb = "runas";
proc1.Arguments = "/c "+anyCommand;
proc1.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(proc1);
我有以下方法,用于从c#运行命令提示符命令
在第一个参数中传递要运行的命令
public static string RunCommand(string arguments, bool readOutput)
{
var output = string.Empty;
try
{
var startInfo = new ProcessStartInfo
{
Verb = "runas",
FileName = "cmd.exe",
Arguments = "/C "+arguments,
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardOutput = true,
RedirectStandardError = false
};
var proc = Process.Start(startInfo);
if (readOutput)
{
output = proc.StandardOutput.ReadToEnd();
}
proc.WaitForExit(60000);
return output;
}
catch (Exception)
{
return output;
}
}