Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Line Break using Clipboard SetText

Tags:

c#

How can I add line breaks using the SetText method?

I tried Clipboard.SetText("eee \n xxxx"); but it doesn't give me the expected result when I paste the clipboard data in the notepad.

Expected result:

eee
xxxx

How can I accomplish this?

like image 829
yonan2236 Avatar asked Jan 27 '12 12:01

yonan2236


2 Answers

I know this is a bit late... but I often use StringBuilder for memory and performance reasons and it' has a very handy AppendLine() method.

like image 127
ebol2000 Avatar answered Oct 18 '22 09:10

ebol2000


Windows uses CR+LF pairs to indicate a new line. This equates to "\r\n" in C#. However, you are just sending "\n", i.e. a single LF rather than a CR+LF pair.

Use Environment.NewLine rather than of "\n". This is the idomatic way to spell "\r\n" in C#. As a bonus, if you ever ran your code on a *nix platform, Environment.NewLine would evaluate to "\n" which is the *nix new line indicator. Finally, in my view Environment.NewLine is preferable from a readability perspective. It documents what this thing is logically rather than relying on you knowing the magic constants.

like image 44
David Heffernan Avatar answered Oct 18 '22 09:10

David Heffernan