c# - Get untitled notepad contents -


i looking solution in c# or in language following:

1) let have opened notepad , have write inside. file unsaved.

2) via program create save "foo.txt" notepad file , close it.

in c# can process name or id can have process. how make process save , close? or maybe @ least data of notepad , can save via systemio. problem how process data of process , in particular example notepad text (remember text unsaved no way recover path).

thanks lot.

or maybe @ least data of notepad

as others have said, it's not best approach far...

...but sure, can that.

here's example retrieves contents of open notepad instances , spits them out in 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 specific notepad (it's not generic approach application). we're using findwindowex() find child window called "edit", direct child of main application window. can use tools spy++ figure out window hierarchy of application solve problems these. in situations target window buried more deeply, or may 1 of many windows of same type @ particular level, may need use several other apis handle correct window. complex topic , there several other low level api approaches can used.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -