How to run batch file in textbox in C# - c#

I want to run batch file but not in the console window. I want to run it in textbox in my WPF c# application. Is that possible? How can i do that?

I believe this can help.
process.CreateNoWindow = true;
process.OutputDataReceived += (s, e) => myMethod(e);
process.BeginOutputReadLine();
You can find details here

You can run the batch file in a separate process and capture the output of that process to display in a WPF control.
For more information and sample code, see Process.BeginOutputReadLine in MSDN.

Related

c# exe encapsulation in wpf not work

I need to encapsulate exe on my wpf application.
My wpf application is very large and with many UserControls.
To do this, i've start the exe from my code, then get the handle and used the "setParent" to "bind" the exe to my application, but the only effect is to show the drop down menu of the exe, but not the main page. For example: i've tried to embedded notepad, but appear only the drop down menu when I click in the area (note that not appear the main menu bar).
var procInfo = new System.Diagnostics.ProcessStartInfo(this.exeName);
procInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(this.exeName);
// Start the process
_childp = System.Diagnostics.Process.Start(procInfo);
// Wait for process to be created and enter idle condition
_childp.WaitForInputIdle();
// Get the main handle
_appWin = _childp.MainWindowHandle;
// Get main window handle
var helper = new WindowInteropHelper(Window.GetWindow(this.AppContainer));
// Incapsulate
SetWindowLongA(_appWin, -20, 0x00000040 | 0x00000008);
SetParent(_appWin, helper.Handle);
Note that I've tried this piece of code in other c# application and work fine!
I think there is a problem of redraw/update the viewport.
In which way can i force this redraw of the external exe inside my application?
Can you help me, even to found an alternative solution to embedded the exe? Thanks
I've tried the solution to run the exe in a separate tab (here), but even this solution not work.
Can I resolve this with a "SendMessage" ???
Can you suggest me a test to do?
I ask you one thing: help me!!!
The below works for me, can provide you with example project if necessary. The missing piece seems to be that either you have a z index issue OR your initial window placement in your desktop co-oridinates is such that is it outside your 'outer window'.
this will bring it the the from and make it FILL your window:
SetWindowPos(_appWin, default(IntPtr), 0, 0, (int)Application.Current.MainWindow.Width, (int)Application.Current.MainWindow.Height, SetWindowPosFlags.FrameChanged);
The default(IntPtr) is for the ZIndex and says 'bring to front'
You can then make that smaller by passing in the offsets from your containing control, ie if this.grid was what you wanted notepad to appear over:
var desiredPos = this.grid.TranslatePoint(new Point(0, 0), Window.GetWindow(this.grid));
SetWindowPos(_appWin, default(IntPtr),
(int)desiredPos.X, (int)desiredPos.Y,
(int)this.grid.ActualWidth, (int)this.grid.ActualHeight, SetWindowPosFlags.FrameChanged);
Using AllowsTransparency="False" instead AllowsTransparency="True" inside the WPF of the Window I've been able to solve partially the problem
Now I've embedded the external exe (for example: "notepad.exe") using this approach (WindowsFormHost approach):
System.Windows.Forms.Panel _pnlSched = new System.Windows.Forms.Panel();
System.Windows.Forms.Integration.WindowsFormsHost windowsFormsHost1 =
new System.Windows.Forms.Integration.WindowsFormsHost();
windowsFormsHost1.Child = _pnlSched;
_grid.Children.Add(windowsFormsHost1);
ProcessStartInfo psi = new ProcessStartInfo(#"notepad.exe");
psi.WindowStyle = ProcessWindowStyle.Normal;
Process PR = Process.Start(psi);
PR.WaitForInputIdle();
SetParent(PR.MainWindowHandle, _pnlSched.Handle);
Now the new problem may be the Z-order of the user control. In fact, when another user contol move above the "notepad", it is below and not above...
enter image description here
Note that also the background of the WindowsFormHost not respect the 'z-order':
enter image description here
any suggestion is welcome
Thanks

Controlling and copying text from another application in Windows CE

I've been using a windows CE application that produces a text in its textbox when its button was clicked. I need this text for my C# app. How can I trigger its button and copy its text to my application.
private void button1_Click(object sender, EventArgs e)
{
Process myProcess = new Process();
try
{
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName = "\\Program Files\\kangkong\\Barcode2005.exe";
myProcess.Start();
// minimize Barcode2005.exe and the code goes here...
}
catch
{
throw;
}
}
Anyway, I'm using C# in Windows CE
Thanks :)
This is similar to your other question c# how to get handle over a specific mainmenu using coredll.dll
You need to define some p/invokes to be able to use the native FindWindow, GetWindowText functions.
First use FindWindow with the class name and/or title of the foreign window to get a handle to the main window. You may also use the processID (pID) to identify the foreign window.
Then enumerate all child windows of the main window until you find the window of the textbox (in native API an TextBox is using the EDIT class and is just a window like everything else you see). Then you can use GetWindowText using the found handle of the EDIT window.
If the foreign window is a dialog, you have to add GetDlgItem to your p/invoke list. Then you can use the EDIT dlg item's control ID to identify the input field and then use GetWindowText using this handle. AFAIR the control IDs are available in the resources of an windows exe file (use ResHacker to get the IDs).
BTW: where comes barcode2005.exe come from? Can you upload a copy for analysis? Is this a barcode graphic generator or decoder? If so, there are easier ways to get barcode graphics or decodes in C#.

How can i create an OpenFileDialog with Radiobuttons? [duplicate]

I am working on winforms application in C#. What I want to achieve is to get a file from user for which I am using the following code:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
string sFileName = dlg.FileName;
//my code goes here
}
Now, everything is working fine but I want to put 3 radio buttons in the same dialog box, meaning I would now get two things from this dialog box
string sFileName = dlg.FileName; //same as in case of traditional dialog box
//some thing like this which tells which radio button is selected:
dlg.rbTypes.Selected
How do I achieve this?
Yes, that's possible, I did the same kind of customization with SaveFileDialog successfully and it's pretty interesting.
Follow the following links:
http://www.codeproject.com/KB/dialog/OpenFileDialogEx.aspx
http://www.codeproject.com/KB/cs/getsavefilename.aspx
http://www.codeproject.com/KB/dialog/CustomizeFileDialog.aspx
Also my own questions too will help you:
Change default arrangement of Save and Cancel buttons in SaveFileDialog
How to stop overwriteprompt when creating SaveFileDialog using GetSaveFileName
You have to use the WinAPI for this and you need to write the ShowDialog method in your own calling the GetOpenFileName windows function inside it, instead of calling .net's OpenFileDialog. The GetOpenFileName will create the windows OpenFileDialog. (Refer to http://msdn.microsoft.com/en-us/library/ms646927%28v=vs.85%29.aspx). This together with writing the HookProc procedure and catching events such as WM_INITDIALOG, CDN_INITDONE inside it will help you do what you want.
To add radio buttons etc., you have to call the windows functions such as CreateWindowEx and SendMessage....
The 2nd link has the exact direction to the customization...
Ask for any clarifications...
On XP you need to use the hook procedure method and the GetOpenFileName API. On Vista and later this will result in a horrid looking file dialog with limited utility, e.g. no search. On Vista you should use IFileDialog and to customise the dialog you need the IFileDialogCustomize interface. Because the new Vista dialogs are exposed as COM interfaces they are quite easy to consume in .net.

How to make a .txt appear after clicking a button

I asked this question a minute ago and was not specific enough so let me try again.
I am trying to generate a report of inventory information that is already made and have it update from the user input into text boxes on the form and then have a button to make the .txt file of the report show to the screen and have the updated information on it.
I have the GUI created and have the button created and the .txt file is created. I just need to know how to make it where I can click the button and have the .txt file appear to the screen.
Using System.Diagnostics;
...
String filename = "C:\\....\data.txt"; \\ File Created With Information
Process.Start(filename); \\ Will open file with default program
The above code can be used to open an external program to display your text file.
As usual I recommend using try/catch since you are dealing with external I/O (files).
You can just start the notepad process with your *.txt file as the argument and start the process can't you?
Found this link that might help you: http://www.csharp-station.com/HowTo/ProcessStart.aspx
Assign a click event to your button (in your class constructor for instance):
button.Click += new EventHandler(button_Click);
In the event, start notepad.exe in a new process:
void button_Click(Object sender, EventArgs e) {
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "notepad.exe";
startInfo.Arguments = "C:\Path\To\My\file.txt";
Process.Start(startInfo);
}

Force dialog close in C#

I'm writing a GIS application in C#. A portion of the application allows the user to select a KML file, and then the program will process that file. I'm using an OpenFileDialog, but the problem is that all of the code is executed before the dialog gets closed (and after the user has OK'd the file). It takes quite awhile because the program has to zoom and do other things. Is there a way to close the dialog programmatically before my code is executed?
EDIT: Some code for those who ask.
private void OnKMLFileSet(object sender, CancelEventArgs e)
{
Polygon polygon = KmlToPolygon(openFileDialog2.FileName);
// After this, I no longer need the file, but the dialog stays open until the end of the method
Graphic graphic = new Graphic();
graphic.Geometry = polygon;
textBox1.Text = string.Format("{0:n}", CalculateAreaInSqKilometers(polygon)).Split('.')[0];
textBox2.Text = string.Format("{0:n}", CalculateAreaInSqMiles(polygon)).Split('.')[0];
textBox3.Text = string.Format("{0:n}", CalculateAreaInSqKnots(polygon)).Split('.')[0];
Note polyInfo = new Note("Polygon with nautical area: " + textBox3.Text, polygon);
map.Map.ChildItems.Add(polyInfo);
map.ZoomTo(polygon.GetEnvelope());
}
It sounds like the dialog is actually closed, but it's still "visible" because the main window is busy and hasn't repainted itself yet.
Some ideas:
The easy way: call the Refresh() method on the main form where the dialog is still visible. Always call it immediately after ShowDialog returns.
If loading takes quite a bit of time, it might be desirable to create a pop-up "loading" dialog, possibly with a cancel button. Use the BackgroundWorker class to load the file in a background thread. When the worker is done, the file is loaded and the pop-up window can be closed. Remember not to change anything in the user interface from the background thread without proper synchronization.
EDIT: After looking at the code, I think I see your problem. You're handling the FileOk event. This will have the effect you are trying to avoid. Use the dialog like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK) {
// open file
}
Don't use the FileOk event. I've never had reason to use it before... Also it might be helpful to follow the advice I already gave.

Categories