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.

2

2 Answers 2

3

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

var content = File.ReadAllText("filepath.txt");
Clipboard.SetText(content);
0
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.