-1

I want to copy a data in a text file to the clipboard. This is the same sort of file you could open in Notepad, for example.

I read this other question, it is about copying a particular given string or from textbox in a form.

But I want to copy from a file.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
Kopika
  • 122
  • 15
  • "notepad" is name of most basic text editor in Windows... Are you sure you are talking about "automation" of that executable? Maybe you use "notepad" as synonym of "text file"? – Alexei Levenkov Aug 13 '15 at 06:29
  • If u get happiness by giving minus marks for new users like me.. then do it.. thank u for discouraging us.. and insullting us – Kopika Aug 13 '15 at 06:32
  • @AlexeiLevenkov I want to automatically copy the text.. without opening the notepad – Kopika Aug 13 '15 at 06:36

2 Answers2

4

if you want to copy the content of a text file to Clipboard the use the following code:

Clipboard.SetText(File.ReadAllText("your file path"));

(Original answer, before question edit: If you want to copy the content of a notepad to Clipboard, then just select the text and press ctrl+c. it will copy the text to the Clipboard.)

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

You are probably looking for the text in file you have, that you view using notepad. Notepad is not required to access that text through code. To access the text in the file you can open that file using c# file stream e.g. StreamReader and copy the read text to clipBoard

Reading text into string string and copying to clipboard.

using (StreamReader sr = new StreamReader("TestFile.txt"))
{
   String text = sr.ReadToEnd();
   Clipboard.SetText(text);
}

OR, simply

Clipboard.SetText(File.ReadAllText("TestFile.txt"));
Adil
  • 146,340
  • 25
  • 209
  • 204