0

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.

Badro Niaimi
  • 959
  • 1
  • 14
  • 28
  • This will set the cursor position to whatever you want. There is no way to do this with an exact line without reading the whole file into a data structure of some sort line by line. `myStreamReader.BaseStream.Position = desiredPosition;` – Jacobr365 Apr 13 '16 at 15:12
  • If it's about Word files you actually can set the cursor position with [Word Automation](http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx) – Maximilian Riegler Apr 13 '16 at 15:16
  • 3
    the position in the StreamReader is the cursor's position to write or read string but when I open the file the stream can't handle it, because in another process. – Badro Niaimi Apr 13 '16 at 15:16
  • Indeed, I open Java files , and I need to set position in any editor opened with. – Badro Niaimi Apr 13 '16 at 15:17

2 Answers2

3

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!

Community
  • 1
  • 1
Daniel Centore
  • 3,220
  • 1
  • 18
  • 39
  • 1
    You took the words out of my mouth. – James Wright Apr 13 '16 at 15:24
  • How about getting the default text editor first, because forcing the file to get open with Notepad is not an excellent idea!!!! – Badro Niaimi Apr 13 '16 at 15:26
  • @BadroNiaimi It is not possible with the default text editor. Perhaps you could tell us a little bit more about why you're trying to do this so we could help you come up with an acceptable alternative? – Daniel Centore Apr 13 '16 at 15:27
  • @BadroNiaimi -- I'm sure I don't understand much about other cultures, but in *my* culture, your response would be considered extremely rude. Daniel has taken his own precious time to provide a perfectly-valid answer to your somewhat-vague question. – rory.ap Apr 13 '16 at 15:29
  • you're missing the point @BadroNiaimi . you can do it, if you know what is opening the file and that editor has command line args you can send on start. No matter what, you are going to have to write code per possible editor to place cursor, and it wont be available in all editors. see this question for a possible approach http://stackoverflow.com/q/13755233/1158842 – hubson bropa Apr 13 '16 at 15:30
  • I'm so sorry about my post infos, because I didn't mention all the details but, indeed I need to open some java files. – Badro Niaimi Apr 13 '16 at 15:32
  • @BadroNiaimi What program do you need to open these files with? Why do you need to open these files? – Daniel Centore Apr 13 '16 at 15:33
  • We're telling you that exactly what you're looking for is impossible. You need to give us much more detail so that we can try to come up with an acceptable workaround for your situation. – Daniel Centore Apr 13 '16 at 15:34
  • I don't know the program, because I'm not the one who will use the program. so I need something dynamic, I edited the post, take a second look. – Badro Niaimi Apr 13 '16 at 15:35
  • @BadroNiaimi There is nothing dynamic. Find out the details from the person who will be using it. – Daniel Centore Apr 13 '16 at 15:37
  • is not only one person , is a team so is not possible to cover all the possibilities. – Badro Niaimi Apr 13 '16 at 15:38
  • @BadroNiaimi -- This might sound overly-trite, but in this case it's true: not everything is possible. – rory.ap Apr 13 '16 at 15:39
  • I found one here lol ,http://stackoverflow.com/questions/162331/finding-the-default-application-for-opening-a-particular-file-type-on-windows – Badro Niaimi Apr 13 '16 at 15:40
  • @BadroNiaimi It is absolutely possible to find the default application. However, every application deals with going to an arbitrary cursor position differently. So either you need to handle every text editor in existence, or you need to find out which text editor(s) will be used by this team. – Daniel Centore Apr 13 '16 at 15:44
  • @DanielCentore, I agree , any way you answer is perfect for my question, because I said a txt file it deserves to mark as solution. – Badro Niaimi Apr 13 '16 at 15:47
  • I'm using the phrase "text editor" in the generic sense. I understand you are using Java files, which are a subset of text files. If you find out more details, I'm sure we can help you find a more acceptable solution. – Daniel Centore Apr 13 '16 at 15:52
0

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"...

rory.ap
  • 34,009
  • 10
  • 83
  • 174