-4

I am looking for a solution in c# or generally in any language which will do the following:

1) Let say you have an opened notepad and have write something inside. File is unsaved.

2) Via the program you will create save as "foo.txt" the notepad file and then close it.

In C# you can get process by name or id so you can have the process. But then how to make the process to save as and then close? Or maybe at least get the data of notepad and then i can save it via SystemIO. But the problem is how from process get the data of the process and in my particular example get the notepad text (remember text is unsaved so no way to recover it from a path).

Thanks a lot.

Nick Doulgeridis
  • 583
  • 1
  • 16
  • 31
  • 2
    What's your reason for doing this? – AStopher Jun 10 '15 at 12:45
  • Im really curious why you want to do this – The_Monster Jun 10 '15 at 12:48
  • because there a many opened untitled notepads and want to save and close at once – Nick Doulgeridis Jun 10 '15 at 12:49
  • 2
    I suspect Notepad would have to expose this information in some way, which it likely does not since I can't imagine the authors ever thought anybody would need to do this. If this is a purely academic exercise, you may want to try something else entirely. If this is an actual requirement for something, I suspect it's the result of a previous error in the design and the approach is flawed. – David Jun 10 '15 at 12:49
  • You will need to hack the program to do such things – Binkan Salaryman Jun 10 '15 at 12:49
  • 2
    Why not make your own text editor? Doing so is quite simple. – AStopher Jun 10 '15 at 12:50

2 Answers2

4

Or maybe at least get the data of notepad

As the others have said, it's not the best approach by far...

...but sure, you can actually do that.

Here's an example that retrieves the contents of all open Notepad instances and spits them out in the Console:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }

    private const int WM_GETTEXT = 0xd;
    private const int WM_GETTEXTLENGTH = 0xe;

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

    [System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, StringBuilder lParam);

    private void button1_Click(System.Object sender, System.EventArgs e)
    {
        System.Diagnostics.Process[] ps = System.Diagnostics.Process.GetProcessesByName("notepad");
        foreach(System.Diagnostics.Process p in ps)
        {
            IntPtr editWnd = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", "");
            string sTemp = GetText(editWnd);
            Console.WriteLine(p.MainWindowTitle);
            Console.WriteLine("------------------------------");
            Console.WriteLine(sTemp);
            Console.WriteLine("------------------------------");
            Console.WriteLine("");
        }
    }

    private string GetText(IntPtr hWnd)
    {
        int textLength = SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1;
        System.Text.StringBuilder sb = new System.Text.StringBuilder(textLength);
        if (textLength > 0)
        {
            SendMessage(hWnd, WM_GETTEXT, textLength, sb);
        }
        return sb.ToString();
    }

}

This approach is specific to Notepad (it's not a generic approach to any application). We're using FindWindowEx() to find a child window called "Edit", that is a direct child of the main application window. You can use tools like Spy++ to figure out the window hierarchy of an application to help solve problems like these. In situations where the target window is buried more deeply, or may be one of many windows of the same type at a particular level, you may need to use several other APIs to get a handle to the correct window. This is a complex topic and there are several other low level API approaches that can be used.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
0

You could get the notepad++ source code and just write a plugin to get the text. Although notepad++ is written in C++ (you can still use visual studio).

You won't be able to do what you want with the standard windows notepad without hacking it or getting access to its source code.

The github for notepad++: https://github.com/notepad-plus-plus/notepad-plus-plus

Community
  • 1
  • 1
AzNjoE
  • 733
  • 3
  • 9
  • 18