How to make a .txt appear after clicking a button - c#

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);
}

Related

How to simulate pressing save/enter in SaveFileDialog

I'm creating an application to automate some processes. One of them is creating docx file from the dotx template.
Steps are quite easy: app opens MS Word with test.dotx file and SaveAs it to c:\temp as a test.docx. It should be as close to user's actions as possible. When the file is opened (from dotx so it is docx already) all I need is to open SaveAs dialog and push "save" (or just "enter", because focus is set on "save" button).
The problem is how to "hit" the save/enter. I tried SendKeys but I am in ShowDialog() which is waiting for the result and cannot perform SendKeys at the moment. Of course if I press enter from keyboard or cklick on "Save" all works perfectly, but this one "press" I'd like to do from the code. Could you please point me how to solve this (if it is possible at all)? Thank you.
Here is the part of the code I'm strugglig with:
SaveFileDialog saveFileDialog1 = new SaveFileDialog
{
InitialDirectory = #"C:\Temp\",
DefaultExt = "docx",
FileName = "test"
};
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
object FileName = saveFileDialog1.FileName;
doc.SaveAs(ref FileName);
}
If hit the save/enter is your requirement, the recommendation is use Spy++ (this Tool can be installed by Visual Studio Installer) to capture both: "the dialog with save button" and "save button" alias/class; then, programming with C# PInvoke (for example, using FindWindow and FindWindowEx) to send/post message to simulate this "save" button.

Select File and Continue Program In WPF

I've been trying to follow tutorials and various Stack Overflow posts, etc. to implement an OpenFileDialog to select a file. The problem is, it seems I can't get my program to continue with the rest of the logic. Not entirely sure if it has something to do with the fact I'm trying to open the file dialog inside my main window or what. Consider the following snippet:
public MainWindow()
{
InitializeComponent();
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
openFileDialog1.ShowHelp = true;
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
As you can see, I've even tried setting the "Help" value to true before the dialog finishes. I've tried to select both the file name for the file string, etc. but to no avail - the program seems to simply wait after the file is selected from the dialog. Would anyone here have a suggestion for a solution?
Previously I had the same problem with WPF. When you are working with WPF, System.Windows.Form Namespace is not included to your Project references;
and in the other hand actually, there are two OpenFileDialog, the first is System.Windows.Forms.OpenFileDialog (this is what you have) and the second is Microsoft.Win32.OpenFileDialog. if you want to get your code to work you must add System.Windows.Forms into your references:
Solution Explorer -> YourProject -> References (Right Click and Add Reference...) -> Assembly -> Framework -> Find And Select System.Windows.Forms -> OK
and the Next Solution is to use Microsoft.Win32, it's pretty easy. just add this Namespace into your code file and Change your code like this:
string file = "";
// Displays an OpenFileDialog so the user can select a file.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "Files|*.txt;*.out";
openFileDialog1.Title = "Select a File";
// Show the Dialog.
// If the user clicked OK in the dialog and
// a file was selected, open it.
if (openFileDialog1.ShowDialog() == true)
{
file = openFileDialog1.FileName;
//file = openFileDialog1.OpenFile().ToString();
//openFileDialog1.Dispose();
}
openFileDialog1 = null;
Console.WriteLine("File path is: " + file);
OpenFileDialog.ShowDialog() is a modal method:
FileDialog is a modal dialog box; therefore, when shown, it blocks the
rest of the application until the user has chosen a file. When a
dialog box is displayed modally, no input (keyboard or mouse click)
can occur except to objects on the dialog box. The program must hide
or close the dialog box (usually in response to some user action)
before input to the calling program can occur.
This means that invoking this method will block your main thread until the dialog box is closed. A few of the options you have are:
Invoke the FileDialog after the main window is initialized.
Invoke the FileDialog from a thread. In this case, be careful to synchronize.

How to run batch file in textbox in 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.

Launch an msi from a button using forms

I feel really silly having to ask this question as I know I should not be having so much trouble with this simple task....but I am trying to launch my .msi when a user pushes a button of a form. I am certain this is a one liner but I cannot for the life of me figure this out. I have the .MSI file on my desktop so I want the button to also be able to have the user select where the msi file is. If anyone could help me that would be grand...
Look at Process.Start.
Process.Start("path to msi");
To get the path to the file, you can use the FileDialog class (assuming winforms).
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
Process.Start(openFileDialog1.FileName);
}
Look at using this to get the file:
FileDialog dialog = new FileDialog();

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