如何使用C#将剪贴板数据粘贴到记事本上 [英] How to paste the clipboard data on notepad using C#
问题描述
亲爱的所有人,
我正在Windows应用程序上工作,我需要将所有文本从文本框中复制到记事本文件中.我可以将所有文本从文本框复制到剪贴板,但是现在如何将剪贴板文本复制到记事本文件.
任何形式的链接,建议和特别的专家建议都将受到高度赞赏.
谢谢与问候,
Balkrishna Raut
Dear All,
i am working on windows application and i need to copy all the text from text box to notepad file. i can copy all the text from text box to clipboard but now how can i copy this clipboard text to Notepad file.
Any kind of link, suggestion and specially expert advice would be highly appreciated.
Thanks & Regards,
Balkrishna Raut
推荐答案
我不建议这样做.
但是,如果您坚持要求,则需要执行以下操作.通过P/Invoke使用Windows APISendInput
,激活记事本并使用SendInput
模拟Ctrl + V键盘按下.
—SA
I don''t recommend doing anything like that.
However, if you insist, you need to do the following. Use Windows APISendInput
via P/Invoke, activate Notepad and simulate Ctrl+V keyboard press usingSendInput
.
—SA
您可以简单地执行以下操作:
You can simply do this :
File.AppendAllText(@"C:\MyNotepadFile.txt",Clipboard.GetText());
关于此方法的最好之处在于,即使文件不存在,它也会创建一个.
如果要覆盖内容,可以使用此方法:
The best thing about this method is that, even if the file doesn''t exist, it will create one.
If want to overwrite the content, you can use this :
File.WriteAllText(@"C:\MyNotepadFile.txt",ClipBoard.GetText());
您似乎想要的是将文本框的内容写入文本文件(记事本文件" =纯文本) ).剪贴板是无关紧要的.
What you appear to want is to write the contents of a text box to a text file (''Notepad file'' = plain text). The clipboard is an an irrelevant distraction.
File.WriteAllText(@"C:\MyNotepadFile.txt", myTextBox.Text);
(感谢其他张贴者提醒我该方法)
然后,如果要在记事本中打开文件,则可以使用Process.Start对文件进行外壳处理.但是我不知道为什么要这么做,因为用户已经可以在文本框中看到内容了.
(thanks to other posters for reminding me of that method)
You can then shell the file using Process.Start if you want to have it open in Notepad. But I have no idea why you would want to do that, since the user can see the content in your textbox already.
这篇关于如何使用C#将剪贴板数据粘贴到记事本上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!