简体   繁体   English

在文件内设置光标位置

[英]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. 如您所见,打开的文件是另一个过程,如果还有另一种使用C#打开文件的方法,则该方法可以为我提供在文件内设置光标位置的可能性。

For example: 例如:

Opening the file at the 20th line , or the 200th char. 在第20行或第200个字符处打开文件。 I want to open files that have .java extension, so I want to use the default program to handle my file. 我想打开扩展名为.java的文件,所以我想使用默认程序来处理我的文件。

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 ++并使用可用的命令行开关:

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! 执行此处理时,请不要忘记包含换行符(可以为1或2)!

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." 当您使用Process.Start “运行”外部文件时,实际上是将控制权传递给操作系统,并说:“请根据文件扩展名在其默认应用程序中打开该文件”。 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. 在大多数情况下,文本文件会在Notepad.exe中打开,但是如果您安装了另一个文本编辑器(如我的(UltraEdit)),则很可能会在其中打开它。 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. 例如,可能会配置您的操作系统,以便无论出于何种原因在Windows Media Player中打开txt文件。 There's no notion of a "cursor location"... 没有“光标位置”的概念...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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