2 回答
TA贡献2037条经验 获得超6个赞
您可以执行以下操作,使用剪贴板并管理“notepad.exe”进程(它应该适用于任何文本编辑器):
1)添加此参考:
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
2)打开记事本并粘贴您的数据:
public void OpenNotepad()
{
Clipboard.SetText("All your text");
Process process = Process.Start("notepad.exe");
process.WaitForInputIdle();
SetForegroundWindow(process.MainWindowHandle);
SendKeys.Send("^V");
}
请注意,这不会创建任何临时文件,但我认为这是您想要的行为。
TA贡献1801条经验 获得超16个赞
第一个选项: 我建议向用户显示一个保存对话框。
阅读文档:https ://docs.microsoft.com/en-us/dotnet/framework/winforms/controls/how-to-save-files-using-the-savefiledialog-component
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "Text Image|*.txt";
saveFileDialog1.Title = "Save the File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if(saveFileDialog1.FileName != "")
{
/// Write a text file here at path saveFileDialog1.FileName
}
第二个选项(如果您仍想坚持使用临时文件):
我建议使用临时文件夹。
string windowsTempPath = Path.GetTempPath();
var filePath = Path.Combine(windowsTempPath, "file_" + DateTime.Now.ToFileTimeUtc() + ".txt");
//// Write txt file
//// For opening file, use Process.Start
Process.Start(filePath);
这将启动默认程序以打开 txt 文件。你不会对记事本或记事本++有任何依赖。
希望这可以帮助。
- 2 回答
- 0 关注
- 294 浏览
添加回答
举报
