1

I would like to open Notepad++ and let it display some unsaved text programmatically from my application. My first approach was to save a text file and then let notepad++ open it.

Process.Start("notepad++.exe", filename);

But I don't wanna spam the file system with random files. And when the user edits the opened text file and saves it, I don't wanna go and try to remove unedited files that my app created. I thought "open notepad++, throw the text inside, and be done with it" would be the cleanest way to do it. FYI: The text is not supposed to come back. It's a one-way-transaction.

I could open notepad++ and send some keystrokes to the system (e.g. text to the clipboard, Ctrl+V in notepad++). But that is unclean and relies on well handled window focusing. Is there another way to make notepad++ display text that has no file or filename?

I tried to find something in notepad++ comand line options. And ghost typing doesn't work for me at all.

Edit: I don't want to do ANY text editing in my app. So it's just about sending text to notepad++.

Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • there are so many apps that do the same thing as yours like FTP/SSH file explorers and many compressors. When you choose to edit some file with an external editor they'll do something and when the file is saved some notification will be sent back so that the file can be updated on the remote side/inside the archive – phuclv Sep 21 '18 at 09:44
  • @phuclv Ok. But I'm not trying to replicate a file explorer or anything. The app is finished. I just want to improve that one functionality "sending text to notepad". I added an FYI to the question. – Bitterblue Sep 21 '18 at 09:57
  • I'm not telling you that you create an explorer or something. I just give examples and you can just look at how those apps implemented that feature. Obviously it's just a simple feature in an app because there's no editing in the original app. It just opens some file in an external app and tells the OS to notify when the file was closed/save – phuclv Sep 21 '18 at 14:31

2 Answers2

2

Disclaimer: This works, but I don't know how good of a solution it is, use at your own risk

I haven't been able to get to work with Notepad++ but with the standard Windows Notepad (which in my opinion is better anyway since you can't assume that Notepad++ is installed) and here is my code:

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

    [STAThread]
    static void Main(string[] args)
    {
        Process notepad = Process.Start("notepad");
        Thread.Sleep(50);

        if (notepad != null)
            SendMessage(FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null), 0x000C, 0, "Hello World");
    }

Explanation: I'm starting a new Notepad with Process.Start("notepad") and then using the two Methods I imported am sending a message to that process to write the text. You can replace the "Hello World" in the last line with any string you want (e.g Clipboard.GetText() to use the text stored in the Clipboard)

MindSwipe
  • 7,193
  • 24
  • 47
  • Related: [SendMessage to Notepad++ in C#](https://stackoverflow.com/q/42402580/205233) - be aware you'r sending UTF-16 strings from C# so you'll have to use `SendMessageW` – Filburt Sep 21 '18 at 09:28
0

EDIT: Answer outdated; My answer was an option until OP changed the question that he doesn't want to save files /EDIT

I had a similar "problem" some days ago and I was not able to solve it without saving the file.

So basically, I just created the a file

File.WriteAllText(DIRECTORYPATH + FILENAME, TextToShow);

Then I opened the process

Process.Start(NOTEPAD++PATH, FILEPATH);

Then I called

Thread.Sleep(500);

Because with larger files I had the problem that notepad opened but told me the file is not available (500 is 0,5 seconds)

In the end I called

File.Delete(FILEPATH);

With this, you can open notepad, put the input inside, and delete the file while notepad remains open with the content

NOTE: This is my own approach to the problem, there may be better ways, but with this you don't need third party libraries for example

Shmosi
  • 322
  • 2
  • 17
  • Why am I getting downvoted? At least tell me whats wrong with my answer – Shmosi Sep 21 '18 at 08:57
  • I've been thinking about it. This will also spawn a dialog in notepad++ about keeping the file in the editor. – Bitterblue Sep 21 '18 at 09:30
  • Sorry about that, I've been working with Notepad2 which doesn't have this dialog – Shmosi Sep 21 '18 at 10:01
  • Yeah, sorry about the downvotes. I gave you a +1 to compensate. Your approach is a valid way to do it. Stackoverflow is not the same as it was before. I ask a question, it starts to be downvoted. – Bitterblue Sep 21 '18 at 10:02
  • The rep is not that important to me, I just like to know why I'm being downvoted to improve myself for future answers :) – Shmosi Sep 21 '18 at 10:04
  • I downvoted because it doesn't answer the question that was asked. The question asked how to do this without saving to file. You saved to file. – David Heffernan Sep 21 '18 at 10:26
  • @DavidHeffernan Well my answer was BEFORE he edited the question saying it shouldn't be saved as a file (look into the question editing) – Shmosi Sep 21 '18 at 11:12
  • Er, your answer was posted after revision 2 which has a long para on the subject, but even in revision 1 it says, "is there another way to make notepad++ display text that has no file or filename?" Also, I don't really think it's a very great answer anyway. And the question is very poor which tends to lead to a flurry of downvotes. – David Heffernan Sep 21 '18 at 11:30
  • On a side note: this answer can be useful to many people who might come here via google. And it might be the exact solution for their problems. That's what happens for me a lot. – Bitterblue Sep 21 '18 at 11:52
  • Thanks for that, that's why I "marked" it as outdated instead of deleting it as a whole :) – Shmosi Sep 21 '18 at 11:55