명령 프롬프트 명령 실행
C # 응용 프로그램 내에서 명령 프롬프트 명령을 실행하는 방법이 있습니까? 그렇다면 다음을 어떻게 하시겠습니까?
copy /b Image1.jpg + Archive.rar Image2.jpg
이것은 기본적으로 JPG 이미지 내에 RAR 파일을 포함합니다. C #에서이 작업을 자동으로 수행하는 방법이 있는지 궁금합니다.
이것이 C #에서 셸 명령을 실행해야하는 전부입니다.
string strCmdText;
strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
편집하다:
cmd 창을 숨기는 것입니다.
System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
startInfo.FileName = "cmd.exe";
startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
process.StartInfo = startInfo;
process.Start();
편집 : 2
중요한 것은 논쟁이 시작 /C
되지 않으면 작동하지 않는다는 것입니다. Scott Ferguson이 말한 방법 : "문자열에 지정된 명령을 수행 한 다음 종료합니다."
@RameshVel 솔루션을 시도했지만 콘솔 응용 프로그램에서 인수를 전달할 수 없습니다. 누군가가 동일한 문제를 겪는다면 해결책이 있습니다.
using System.Diagnostics;
Process cmd = new Process();
cmd.StartInfo.FileName = "cmd.exe";
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.UseShellExecute = false;
cmd.Start();
cmd.StandardInput.WriteLine("echo Oscar");
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
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);
기술적으로 이것은 제시된 질문에 직접적으로 대답하지는 않지만 원본 포스터가 원하는 작업 인 파일 결합을 수행하는 방법에 대한 질문에 대답합니다. 이 글은 초보자가 Instance Hunter와 Konstantin이 말하는 내용을 이해하는 데 도움이되는 게시물입니다.
이것은 파일을 결합하는 데 사용하는 방법입니다 (이 경우 jpg 및 zip). zip 파일의 내용으로 채워지는 버퍼 (한 번의 큰 읽기 작업이 아닌 작은 청크)를 만든 다음 zip 파일의 끝이 끝날 때까지 버퍼가 jpg 파일의 뒷면에 기록됩니다. 도달 :
private void CombineFiles(string jpgFileName, string zipFileName)
{
using (Stream original = new FileStream(jpgFileName, FileMode.Append))
{
using (Stream extra = new FileStream(zipFileName, FileMode.Open, FileAccess.Read))
{
var buffer = new byte[32 * 1024];
int blockSize;
while ((blockSize = extra.Read(buffer, 0, buffer.Length)) > 0)
{
original.Write(buffer, 0, blockSize);
}
}
}
}
예, (Matt Hamilton의 의견에있는 링크 참조) 있지만 .NET의 IO 클래스를 사용하는 것이 더 쉽고 좋습니다. File.ReadAllBytes를 사용하여 파일을 읽은 다음 File.WriteAllBytes를 사용하여 "포함 된"버전을 작성할 수 있습니다.
None of the above answers helped for some reason, it seems like they sweep errors under the rug and make troubleshooting one's command difficult. So I ended up going with something like this, maybe it will help someone else:
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();
You can do this using CliWrap in one line:
var stdout = new Cli("cmd")
.Execute("copy /b Image1.jpg + Archive.rar Image2.jpg")
.StandardOutput;
Here is little simple and less code version. It will hide the console window too-
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg";
process.Start();
with a reference to Microsoft.VisualBasic
Interaction.Shell("copy /b Image1.jpg + Archive.rar Image2.jpg", AppWinStyle.Hide);
if you want to keep the cmd window open or want to use it in winform/wpf then use it like this
string strCmdText;
//For Testing
strCmdText= "/K ipconfig";
System.Diagnostics.Process.Start("CMD.exe",strCmdText);
/K
Will keep the cmd window open
You can achieve this by using the following method (as mentioned in other answers):
strCmdText = "'/C some command";
Process.Start("CMD.exe", strCmdText);
When I tried the methods listed above I found that my custom command did not work using the syntax of some of the answers above.
I found out more complex commands need to be encapsulated in quotes to work:
string strCmdText;
strCmdText = "'/C cd " + path + " && composer update && composer install -o'";
Process.Start("CMD.exe", strCmdText);
you can use simply write the code in a .bat
format extension ,the code of the batch file :
c:/ copy /b Image1.jpg + Archive.rar Image2.jpg
use this c# code :
Process.Start("file_name.bat")
참고URL : https://stackoverflow.com/questions/1469764/run-command-prompt-commands
'IT박스' 카테고리의 다른 글
Google 크롬에서 HTTP 헤더를 보시겠습니까? (0) | 2020.10.04 |
---|---|
내 시스템에서 RVM (Ruby 버전 관리자)을 제거하려면 어떻게해야합니까? (0) | 2020.10.04 |
메서드 반환 유형을 제네릭으로 만드는 방법은 무엇입니까? (0) | 2020.10.04 |
64 비트 JVM 또는 32 비트 JVM (프로그램 내에서)에서 실행 중인지 어떻게 알 수 있습니까? (0) | 2020.10.04 |
SQL Server 트랜잭션 로그를 어떻게 지우나요? (0) | 2020.10.04 |