请教:如何生成一个文本文件,然后不保存而是直接打开显示出来?

betagain 2010-07-23 12:38:36
请教大家一个问题,
使用C#开发winform程序

想生成一个文本文件,生成后打开显示出来,如何做?

找到的代码都是要先保存建立一个文件,我不需要保存,就是直接写一行一行的内容,然后显示出来

谢谢!
...全文
123 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-07-23
  • 打赏
  • 举报
回复
如果不怕修改了剪贴板效果不好。可以发粘贴消息,这样可以关闭记事本时候提示是否保存。

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_PASTE = 0x302;
public const int WM_SETTEXT = 0xC;
[STAThread]
static void Main(string[] args)
{
Process p = Process.Start("Notepad.exe");
while (p.MainWindowHandle == IntPtr.Zero) Thread.Sleep(10);
string s = "就写这些文字可以么?\r\nteststring";
IntPtr hEdit = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", "");
Clipboard.SetText(s);
SendMessage(hEdit, WM_PASTE, 0, 0);
Console.ReadKey();
}
兔子-顾问 2010-07-23
  • 打赏
  • 举报
回复
新建个控制台程序,用如下代码替换main可以看到demo效果。

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hWnd1, IntPtr hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, string text);
public const int WM_SETTEXT = 0xC;
static void Main(string[] args)
{
Process p = Process.Start("Notepad.exe");
while (p.MainWindowHandle == IntPtr.Zero) Thread.Sleep(10);
string s = "就写这些文字可以么?\r\nteststring";
IntPtr hEdit = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", "");
SendMessage(hEdit, WM_SETTEXT, 0, s);
Console.ReadKey();
}
claylee 2010-07-23
  • 打赏
  • 举报
回复
启动一个进程打开notepad
betagain 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lvfeng19806001 的回复:]
那就用textbox直接显示呗
[/Quote]

用文本显示出来的原因,是使用者看了内容后,可以直接编辑 或者 保存为文本文件
兔子-顾问 2010-07-23
  • 打赏
  • 举报
回复
打开记事本。找到文本框,SendMessage发消息WM_SETTEXT,把文本写进去。
lvfeng19806001 2010-07-23
  • 打赏
  • 举报
回复
那就用textbox直接显示呗
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 betagain 的回复:]
引用 1 楼 lvfeng19806001 的回复:
那就用textbox直接显示呗


用文本显示出来的原因,是使用者看了内容后,可以直接编辑 或者 保存为文本文件
[/Quote]

这跟“生成一个文本文件”有什么关系?

xxccccxxxewe 2010-07-23
  • 打赏
  • 举报
回复
可以先写进一个TXT里面,然后再打开,打开后就可以在记事本里修改保存了。
  • 打赏
  • 举报
回复
“生成一个文本文件,不保存”

我感觉实在无法理解无厘头啊!

111,098

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧