-1

I would like to launch a new notepad instance and write content to it from my desktop app (WPF). After that it is user's discretion to save the file or not. (I know I can launch new notepad instance using System.Diagnostics.Process.Start("notepad.exe"))

Just like the process if some one manually wants to create a .txt file. He first opens notepad from start menu. Then write something and then save in desired folder.

Is it possible?

pasaban
  • 91
  • 1
  • 11

1 Answers1

1

try this one source

 [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 button1_Click(object sender, EventArgs e)
        {
            Process [] notepads=Process.GetProcessesByName("notepad");
            if(notepads.Length==0)return;            
            if (notepads[0] != null)
            {
                IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
                SendMessage(child, 0x000C, 0, textBox1.Text);
            }
        }
Community
  • 1
  • 1
sm.abdullah
  • 1,777
  • 1
  • 17
  • 34
  • Thanks sm.abdullah!!! – pasaban Jun 24 '15 at 08:13
  • One problem... While running the app in step-debug mode the file is getting populated with data, but if I run it through, no data is getting written in the file... any Idea? – pasaban Jun 24 '15 at 08:29
  • did you have multiple instances of note pad ? or make sure you are getting process of notepad. – sm.abdullah Jun 24 '15 at 09:01
  • 1
    No I did not. Just found the solution, - IntPtr child=new IntPtr(); do { child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null); } while (child.ToString() == "66874"); – pasaban Jun 24 '15 at 09:04
  • What I observed is that if the child value is 66874 then it was not able to write the content. And that is happening if the code runs as fast as it should be. It takes time to search and find the exact child value (that's what happening when I step debug). Hence the solution – pasaban Jun 24 '15 at 09:07