在 c# 中,我想启动一个进程,该进程将在任何编辑器中打开一个文本文件,并自动将光标移动到特定的行号。
我可以使用打开一个文件
Process.Start(@"c:\myfile.txt");
但我不知道如何将光标移动到该文件中的特定位置。
用源代码回答:
是的,我用的是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
}
}
最佳答案
获取Notepad++,然后你可以这样做:
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());
在这个例子中,我们从注册表中获取 n++ 的安装路径,构建 exe 和 readme.txt 文件的路径,打开它自己的 readme.txt 文件,光标位于第 20 行。 使用 StringBuilder 比使用多个追加更有效(SO 上某处的解释)。
关于c# - 如何启动将在任何编辑器中打开文本文件并自动将光标移动到特定行号的进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13755233/