8

From c#, I want to launch a process which will open a text file in any editor and automatically move cursor to a certain line number.

I can open a file using

Process.Start(@"c:\myfile.txt");

but I don't know how to move cursor at specific location in that file.


Answer with source code:

yes, I used notepad++

private void openLog() {
            try {
                // see if notepad++ is installed on user's machine
                var nppDir = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Notepad++", null, null);
                if (nppDir != null) {
                    var nppExePath = Path.Combine(nppDir, "Notepad++.exe");
                    var nppReadmePath = Path.Combine(yourDirectory,fileName );
                    var line = 20;
                    var sb = new StringBuilder();
                    sb.AppendFormat("\"{0}\" -n{1}", nppReadmePath, lineNo);
                    Process.Start(nppExePath, sb.ToString());
                } else {
                    string newPath = @"\\mySharedDrive\notpad++\bin\notepad++.exe";
                    Process.Start(newPath, @"\\" + filePath + " -n" + lineNo); // take exe from my shared drive
                }
            } catch (Exception e) {
                Process.Start(@"\\" + FilePath); // open using notepad
            }
        }
Andan Desai
  • 137
  • 1
  • 10
  • 2
    The only way I think this might be possible is with automation at the level of keyboard driver input, making the assumption that the editor goes down one line for every down arrow pressed. You can take a look at http://www.autohotkey.com/ for that – Aaron Anodide Dec 07 '12 at 00:48
  • 1
    Notepad has "go to line" feature as long as word wrap is disabled. That will still require sending keyboard input to process, which sucks. If you're OK using notepad++ then check out http://superuser.com/questions/290409/opening-a-text-file-at-a-certain-line-shortcut. Notepad++ seems to have a commandline arg which will open it at a specific line. – Mike Trusov Dec 07 '12 at 00:53
  • any final solution with full source code? Maybe using Notepad++ and UltraEdit too. – Kiquenet Mar 04 '13 at 13:49
  • yeah, have updated it above – Andan Desai Mar 26 '13 at 19:00

3 Answers3

11

Get Notepad++, then you can do this:

    var nppDir = (string)Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Notepad++", null, null);
    var nppExePath = Path.Combine(nppDir, "Notepad++.exe");
    var nppReadmePath = Path.Combine(nppDir, "readme.txt");
    var line = 20;
    var sb = new StringBuilder();
    sb.AppendFormat("\"{0}\" -n{1}", nppReadmePath, line);
    Process.Start(nppExePath, sb.ToString());

In this example we get install path of n++ from the registry, build path to exe and readme.txt file, opens its own readme.txt file with cursor on line 20. Using StringBuilder is more efficient than using multiple appends (explanation somewhere on SO).

Mike Trusov
  • 1,958
  • 1
  • 15
  • 23
  • great, this works thanks a lot. by the way since u knew this, I guess you would also know a way to find "notepad++.exe" location dynamically and quickly ? – Andan Desai Dec 07 '12 at 02:01
  • If it was installed using the installer you can get it from the Registry: var path = Registry.GetValue("HKEY_LOCAL_MACHINE\\SOFTWARE\\Notepad++", null, null); – Mike Trusov Dec 07 '12 at 02:22
2

The solution very heavily depends on which process/editor is opened on your system. That editor would have to have a developer API that you could use to access functionality such as setting ranges and altering the cursor position. For example, if the editor that is opened is Microsoft Word, you would use the Word Interop API to set a selection at a specific position. There is no universal way to do this in 'any editor' since each one has its own API (or no outward facing API at all).

Community
  • 1
  • 1
Vlad Magdalin
  • 1,692
  • 14
  • 17
  • yes, asking for "any" is probably asking for magic. Thanks for Word Interop API. I will give it a try. Since I so heavily rely on line no. I doubt opening the file with MSWord will work. – Andan Desai Dec 07 '12 at 02:05
1

Perhaps you are going this the wrong way. I'm not sure what you are trying to accomplish, but I think it would be alot easier to just open the text file in an editor that belongs to your application. Perhaps another form with a WYSIWYG editor control. That way you have full control on where the cursor will land in that editor. Otherwise, there are just way too many unknowns for anything feasibly workable.

System Down
  • 6,192
  • 1
  • 30
  • 34
  • i m just writing a small key word search utility tool which can search from logs across all network servers, and on request of user I want to open that log file @specific line no. I am not sure rendering those large logs in an embedded control with thin the application. Is it elegant ? – Andan Desai Dec 07 '12 at 01:56
  • 1
    Since logs are (usually) read-only, you could just extract the relevant lines (and X surrounding lines) and display them locally in your application. Opening a text editor sounds like overkill. – Vlad Magdalin Dec 07 '12 at 02:38