Fire Fist
Fire Fist

Reputation: 7050

How to open text in Notepad from .NET?

When I click a button on a Windows Forms form, I would like to open a Notepad window containing the text from a TextBox control on the form.

How can I do that?

Upvotes: 25

Views: 47928

Answers (5)

David Wiseman
David Wiseman

Reputation: 136

I was using the NotepadHelper solution until I discovered it doesn't work on Windows 11. Writing the file to disk and starting with the default text editor seems to be the best solution. This has already been posted, but I discovered you need to pass UseShellExecute=true.

System.IO.File.WriteAllText(path, value);
System.Diagnostics.ProcessStartInfo psi = new() { FileName = path, UseShellExecute = true };
System.Diagnostics.Process.Start(psi);

I write to the System.IO.Path.GetTempPath() folder and run a cleanup when the application exits - searching for a unique prefix pattern for file names used by my app. Something like this:

string pattern = TempFilePrefix + "*.txt";
foreach (string f in Directory.EnumerateFiles(Path.GetTempPath(), pattern))
{
    File.Delete(f);
}

Upvotes: 0

s k
s k

Reputation: 5202

For non ASCII user.

[DllImport("User32.dll", EntryPoint = "SendMessage", CharSet = CharSet.Unicode)]
private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

Based on @Peter Mortensen answer

Add CharSet = CharSet.Unicode to the attribute for supporting Unicode characters

Upvotes: 0

kmatyaszek
kmatyaszek

Reputation: 19296

You don't need to create file with this string. You can use P/Invoke to solve your problem.

Usage of NotepadHelper class:

NotepadHelper.ShowMessage("My message...", "My Title");

NotepadHelper class code:

using System;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace Notepad
{
    public static class NotepadHelper
    {
        [DllImport("user32.dll", EntryPoint = "SetWindowText")]
        private static extern int SetWindowText(IntPtr hWnd, string text);

        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

        public static void ShowMessage(string message = null, string title = null)
        {
            Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
            if (notepad != null)
            {
                notepad.WaitForInputIdle();

                if (!string.IsNullOrEmpty(title))
                    SetWindowText(notepad.MainWindowHandle, title);

                if (!string.IsNullOrEmpty(message))
                {
                    IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
                    SendMessage(child, 0x000C, 0, message);
                }
            }
        }
    }
}

References (pinvoke.net and msdn.microsoft.com):

SetWindowText: pinvoke | msdn

FindWindowEx: pinvoke | msdn

SendMessage: pinvoke | msdn

Upvotes: 55

Oded
Oded

Reputation: 499002

Save the file to disk using File.WriteAllText:

File.WriteAllText("path to text file", myTextBox.Text);

Then use Process.Start to open it in notepad:

Process.Start("path to notepad.exe", "path to text file");

Upvotes: 5

Nick Rolando
Nick Rolando

Reputation: 26167

Try this out:

System.IO.File.WriteAllText(@"C:\test.txt", textBox.Text);
System.Diagnostics.Process.Start(@"C:\test.txt");

Upvotes: 51

Related Questions