3

I'm creating an application where a user would fill in a bit of code (for example a SteamID), and whenever the user clicks the button, it will be copied to the clipboard.

The way it works is simple, the first time the program runs, and it detects both .txt's in the install dir are empty, the user will be prompted to fill that in.

Everything works until here.

Whenever the user clicks the button, I need a way of the program getting all text in the .txt file, and putting this in the users clipboard.

I've tried literally every method here, but it did not work, or I did not understand it as the code I found would only put text in clipboard that has been programmed into the code, and not that was put into a file.

Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
marceltje
  • 107
  • 1
  • 9
  • [Console](http://stackoverflow.com/questions/13571426/how-can-i-copy-a-string-to-clipboard-within-my-console-app-without-adding-a-refe) – Hamid Pourjam Jun 26 '16 at 13:40
  • [Windows Form or WPF](http://stackoverflow.com/questions/3546016/how-to-copy-data-to-clipboard-in-c-sharp) – Hamid Pourjam Jun 26 '16 at 13:40

2 Answers2

3

You need a reference to System.Windows or System.Windows.Forms

var content = File.ReadAllText("filepath.txt");
Clipboard.SetText(content);
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
0

If I understand your question correctly, you are looking to read the content of a text file and then copy it to clipboard

Read from file:

var fileContent= string.Empty;
using (var streamReader = new StreamReader(filePath, Encoding.UTF8)) {            
    fileContent= streamReader.ReadToEnd();
}

OR

var fileContent= File.ReadAllText(filePath);

Copy to clipboard:

Clipboard.SetText(fileContent)

Use namespace System.Windows.Forms for Windows Form or System.Windows for WPF

Ankit Vijay
  • 3,752
  • 4
  • 30
  • 53