Reputation: 300
I want to open a text file and write some data to the file. Here is my code:
FileStream fs1 = new FileStream("D:\\Yourfile.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs1);
writer.Write("Hello");
writer.Close();
System.Diagnostics.Process.Start(@"D:\\Yourfile.txt");
This code works fine.But here first the file is getting saved. I want a text file to open along with the data and let the user save the text file. Is it possible?
Upvotes: 2
Views: 6132
Reputation: 1988
try this will solve your problem
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
private void button6_Click(object sender, EventArgs e)
{
Process[] notepads = Process.GetProcessesByName("notepad");
if (notepads.Length == 0)
{
Process.Start(@"notepad.exe");
Thread.Sleep(100);
}
notepads = Process.GetProcessesByName("notepad");
// return;
if (notepads[0] != null)
{
IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, "Hello");
}
}
Upvotes: 2
Reputation: 67898
If you wanted the user to provide text for the file, then build a Windows Form with a single text box. Let the user type whatever they want, and when you're done, do this:
File.WriteAllText(pathToFile, textBox.Text);
Upvotes: 0
Reputation: 52185
If I am understanding you correctly, you would like your program to open Notepad and put some text in it. Then, the user will decide if the file will be saved or not.
If that is the case, you can use the Process
class to launch notepad. Once you do that, you can fire a series of keyboard events (which mimic keys) so that you will have text.
That being said, what I think could be a cleaner solution would be to open a seperate Form with a text area/text box so that the user can read through. Then, have a button called Save
which essentially does what you are already doing.
Upvotes: 3