简体   繁体   中英

Setting the cursor position inside a file

I just want to know, is there any possibility to set the cursor's position inside a text file, I open the Text file using this code:


if (File.Exists(file))
{
       Process.Start(file);//file is a string that contain the file path
}

As you can see the opened file is another process, if there is another way to open file using C#, a way that can provide me with possibility to set the cursor position inside the file.

For example:

Opening the file at the 20th line , or the 200th char. I want to open files that have .java extension, so I want to use the default program to handle my file.

While it is not possible with a generic text editor, and is probably not possible with the default text editor on your system, it would be possible with many alternative text editors. For instance, you could install notepad++ and use the command line switches available to you:

notepad++ [--help] [-multiInst] [-noPlugins] [-lLanguage]
 [-nLineNumber] [-cColumnNumber] [-xPos] [-yPos]
 [-nosession] [-notabbar] [-ro] [-systemtray] [-alwaysOnTop] 
[-Llanguege code] [-r]

Then you can use code like that from this post to jump to a specific position in the file.

Edit: If you need to get to a specific character position, you could read the file and figure out which line and column number the character you were looking for falls on. Don't forget to include the newline character(s) (there can be 1 or 2) when you do this processing!

No, not possible. When you "run" an external file using Process.Start , you're really passing control to the operating system and saying, "please open this file in its default application based on its file extension." What results, of course, depends on what application you have configured to open your file type.

In most cases, a text file would open in Notepad.exe, but if you've installed another text editor, like I have (UltraEdit), it will most-likely open in that instead. Either way, the operating system doesn't guarantee that it will run any application at all. Beyond that, you have no control over where the cursor, if there even is one, ends up in the target application. Your OS might, for instance, be configured so that txt files open in Windows Media Player for whatever reason. There's no notion of a "cursor location"...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM