如何使用 C# 控制台程序通过记事本打开和保存文件
How to open and save file thru notepad by C# console program
我已经知道我可以在 C# 中通过记事本打开和关闭现有的文本文件
像这样:
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
// open by notepad
Process.Start("notepad.exe", @"myfile.txt");
// automatically save this file by notepad
// kill notepad process
Process[] proc = Process.GetProcessesByName("notepad");
proc[0].Kill();
}
}
但是我需要在关闭这个文件之前用记事本自动保存这个文件。我试图发送击键 ctrl+s 但没有成功。
是否有 .net 代码?谢谢
如果您需要修改文件,更好的方法是使用 System.IO.File-class. When file is opened in code, you can i.e change it's encodings with classes found in System.Text-命名空间 (System.Text.Encoding) 中当前存在的操作打开、修改和保存文本文件内容。
您可以开始 notepad.exe
过程,但之后就没什么可做的了。
我想你可以做这样的事情
SendKeys.SendWait Method (String)
Sends the given keys to the active application, and then waits for the
messages to be processed.
备注
Use SendWait to send keystrokes or combinations of keystrokes to the
active application and wait for the keystroke messages to be
processed. You can use this method to send keystrokes to an
application and wait for any processes that are started by the
keystrokes to be completed. This can be important if the other
application must finish before your application can continue.
进口
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
代码
var process = Process.Start("notepad.exe", @"myfile.txt"));
process.WaitForInputIdle();
var handle = process.MainWindowHandle;
SetForegroundWindow(handle);
// if the window is still in the foreground
SendKeys.SendWait("^(s)"); // Ctrl+S
我已经知道我可以在 C# 中通过记事本打开和关闭现有的文本文件 像这样:
using System.Diagnostics;
class Program
{
static void Main(string[] args)
{
// open by notepad
Process.Start("notepad.exe", @"myfile.txt");
// automatically save this file by notepad
// kill notepad process
Process[] proc = Process.GetProcessesByName("notepad");
proc[0].Kill();
}
}
但是我需要在关闭这个文件之前用记事本自动保存这个文件。我试图发送击键 ctrl+s 但没有成功。 是否有 .net 代码?谢谢
如果您需要修改文件,更好的方法是使用 System.IO.File-class. When file is opened in code, you can i.e change it's encodings with classes found in System.Text-命名空间 (System.Text.Encoding) 中当前存在的操作打开、修改和保存文本文件内容。
您可以开始 notepad.exe
过程,但之后就没什么可做的了。
我想你可以做这样的事情
SendKeys.SendWait Method (String)
Sends the given keys to the active application, and then waits for the messages to be processed.
备注
Use SendWait to send keystrokes or combinations of keystrokes to the active application and wait for the keystroke messages to be processed. You can use this method to send keystrokes to an application and wait for any processes that are started by the keystrokes to be completed. This can be important if the other application must finish before your application can continue.
进口
[DllImport("User32.dll")]
public static extern Int32 SetForegroundWindow(int hWnd);
代码
var process = Process.Start("notepad.exe", @"myfile.txt"));
process.WaitForInputIdle();
var handle = process.MainWindowHandle;
SetForegroundWindow(handle);
// if the window is still in the foreground
SendKeys.SendWait("^(s)"); // Ctrl+S