whats the right c# input stream for cmd.exe encoding - c#

I am starting a cmd.exe from my c# application and redirect its inputstream.This works fine for normal chars like "abc" But when i try to redirect chars like "äöüßáàâ" in the consolewindow appears "õ÷³óô".
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
myProcess.StartInfo.WorkingDirectory = #"c:\";
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
myStreamWriter.WriteLine("äöüßáàâ");
myStreamWriter.Encoding says its encoding is codepage 1252 i tryed to convert my string into it but it didnt change the result.
How to convert my string that it is shown correct?

I took the code and ran the same.. When I didnt redirect the output, I saw the same as the OP. However
Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/K";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardInput = true;
//myProcess.StartInfo.StandardOutputEncoding = Encoding.UTF32;
myProcess.StartInfo.RedirectStandardOutput = true;
myProcess.StartInfo.WorkingDirectory = #"c:\";
myProcess.Start();
StreamWriter myStreamWriter = myProcess.StandardInput;
StreamReader myStreamReader = myProcess.StandardOutput;
myStreamWriter.WriteLine("äöüßáàâ");
richTextBox1.Text = myStreamReader.ReadToEnd();
This produced c:\>äöüßáàâ as expected in the text box.. even though it didnt seem to show it as right in the console window that showed.

Related

Edit image source C#

I'm trying to edit a image file where instead of path i should be able to provide ImageSource as parameter.
//EditCode:
Process proc = Process.Start(Path);
proc.EnableRaisingEvents = true;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Verb = "edit";
proc.StartInfo = startInfo;
proc.Exited += new EventHandler((s, e) => myProcess_Exited(s, e, obj.ToString()));
The above code works well when i pass the Path of the image file as the parameter. But now i have only the ImageSource.
//ImageSourceCode:
private BitmapFrame LoadImage(string path)
{
BitmapDecoder decoder = null;
if (File.Exists(path) && (path != null))
{
using (var stream = new FileStream(path, FileMode.Open, FileAccess.Read))
{
decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
}
return decoder.Frames.FirstOrDefault();
}
else
return null;
}
The above code gets the path and convert an image as a source(BitmapFrame) and returns it.
Now i need this BitmapFrame to be passed in some function and edit the particular image file in paint and save it.
I neeed somthing like this,
Process proc = Process.Start(`ImageSource`);// Instead of path i need to pass the Image Source.
proc.EnableRaisingEvents = true;
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.Verb = "edit";
proc.StartInfo = startInfo;
How can i achieve this?
You can't.
ProcessStartInfo starts a new process that is outside the App Domain of your application. You can't pass it structures in memory within your app domain as a command line parameter.

Printing PDF using ReportViewer from C# windows

Here is my creating PDF code, which will open the PDF document .
public void createPDF(string Reportpath, ReportViewer RV)
{
Warning[] warnings;
string[] streamids;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
byte[] bytes = RV.LocalReport.Render("pdf", null, out mimeType, out encoding, out extension, out streamids, out warnings);
try
{
FileStream fs = new FileStream(Reportpath, FileMode.Create);
Thread.Sleep(1000);
fs.Write(bytes, 0, bytes.Length);
fs.Close();
Thread.Sleep(1000);
System.Diagnostics.Process.Start(Reportpath);
}
catch (Exception ex)
{
MessageBox.Show("Report could not be created...\n" + ex.Message);
}
}
instead of opening i need to print the pdf directly using reportviewer or any other way to print that pdf document??.
I think this MSDN article gives a good solution to your problem
got some idea from the following link
http://www.codeproject.com/Tips/598424/How-to-Silently-Print-PDFs-using-Adobe-Reader-and
send the pdf to adobe reader to print....
public static Boolean PrintPDFs(string pdfFileName)
{
try
{
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";
//Define location of adobe reader/command line
//switches to launch adobe in "print" mode
proc.StartInfo.FileName =
#"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
proc.StartInfo.Arguments = String.Format(#"/p /h {0}", pdfFileName);
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
}
proc.EnableRaisingEvents = true;
proc.Close();
KillAdobe("AcroRd32");
return true;
}
catch
{
return false;
}
}

Read a byte array from a redirected process

I am using the process object in c#
I am also using FFMPEG.
I am trying to read the bytes from the redirected output. i know the data is an image but when I use the following code I do not get an image byte array.
this is my code:
var process = new Process();
process.StartInfo.FileName = #"C:\bin\ffmpeg.exe";
process.StartInfo.Arguments = #" -i rtsp://admin:admin#192.168.0.8:554/video_1 -an -f image2 -s 360x240 -vframes 1 -";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
var output = process.StandardOutput.ReadToEnd();
byte[] bytes = Encoding.ASCII.GetBytes(output);
The 1st bytes are not the header of a jpeg?
I think treating the output as a text stream is not the right thing to do here. Something like this worked for me, just directly read the data off the output pipe, it doesn't need conversion.
var process = new Process();
process.StartInfo.FileName = #"C:\bin\ffmpeg.exe";
// take frame at 17 seconds
process.StartInfo.Arguments = #" -i c:\temp\input.mp4 -an -f image2 -vframes 1 -ss 00:00:17 pipe:1";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
process.Start();
FileStream baseStream = process.StandardOutput.BaseStream as FileStream;
byte[] imageBytes = null;
int lastRead = 0;
using (MemoryStream ms = new MemoryStream())
{
byte[] buffer = new byte[4096];
do
{
lastRead = baseStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, lastRead);
} while (lastRead > 0);
imageBytes = ms.ToArray();
}
using (FileStream s = new FileStream(#"c:\temp\singleFrame.jpeg", FileMode.Create))
{
s.Write(imageBytes, 0, imageBytes.Length);
}
Console.ReadKey();

How send string into a process with UTF-8 encoding using stdin with C#?

I need to send a string using stdin into a process using UTF-8 encoding. I do this:
var info = new ProcessStartInfo("process.exe", "");
info.UseShellExecute = false;
info.CreateNoWindow = false;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
var process = System.Diagnostics.Process.Start(info);
string stringToSend = "человек";
byte[] utf16Bytes = Encoding.Unicode.GetBytes(stringToSend);
byte[] utf8Bytes = Encoding.Convert(Encoding.Unicode, Encoding.UTF8, utf16Bytes);
//But how do I send it now?
//process.StandardInput.WriteLine(stringToSend); //Replaces all non-ASCII chars with ????
but what the process receives is ???? instead of non-English characters.
PS. The Cyrillic word is just to illustrate the example.

StandardInputEncoding for ProcessStartInfo?

When i am adding service to windows manually by typing in CMD something like this:
"C:\Program Files (x86)\Windows Resource Kits\Tools\instsrv.exe" "some-pl-char-ąźńćńół" "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe"
... everything is good with service name, but when i try do that in c#:
ProcessStartInfo startInfo = new ProcessStartInfo();
Process myprocess = new Process();
startInfo.FileName = "cmd";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
myprocess.StartInfo = startInfo;
myprocess.Start();
StreamWriter sw = myprocess.StandardInput;
StreamReader sr = myprocess.StandardOutput;
Thread.Sleep(200);
string command = ...
^ "C:\Program Files (x86)\Windows Resource Kits\Tools\instsrv.exe" "some-pl-char-ąźńćńół" "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe"
sw.WriteLine(command);
sw.WriteLine("exit");
Thread.Sleep(200);
sw.Close();
sr.Close();
then name of created service is: some-pl-char-¦č˝Š˝ˇ-
Why there is problem with code page?
There is something like StandardInputEncoding for ProcessStartInfo?
My active code page in CMD (using chcp) is 852. (Polish)
Arguments belongs assigned to the Arguments property and backslashes needs to be escaped by another one. \ -> \\
Updated:
using (var process = new Process())
{
var encoding = Encoding.GetEncoding(852);
var psi = new ProcessStartInfo();
psi.FileName = "cmd";
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
psi.StandardOutputEncoding = encoding;
process.StartInfo = psi;
process.Start();
using (var sr = process.StandardOutput)
using (var sw = new StreamWriter(process.StandardInput.BaseStream, encoding))
{
var command = "....";
sw.WriteLine(command);
// etc..
}
}
I had a very similar issue. Although I was working with VB.net, it fixed my issue. I couldnt run the command unless this was set.
startInfo.FileName = "cmd.exe /c";
instead of
startInfo.FileName = "cmd";

Categories