'Characters being copied are getting multiplied on the text file on c# windows form
I am creating a simple clipboard system on C# and every time characters or words are copied, they are getting multiplied on the text file just like on the picture below.

here is my code
string path = @"C:\\Users\\" + Environment.UserName + "\\Documents\\clipboard.txt";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path)) ;
if(File.Exists(path))
{
while (true)
{
var text = Clipboard.GetText();
File.AppendAllText(path, text);
Thread.Sleep(2500);
}
}
}
Solution 1:[1]
This is a pretty crude fix and I'm sure that there's a much more efficient way to do it than this, but for the time being this should work.
You'll store the last copied text as lastText and compare it to the current text in text, if they match up then your clipboard hasn't changed, if they don't then you've got new text on your clipboard and should append it to the file.
string path = @"C:\\Users\\" + Environment.UserName + "\\Documents\\clipboard.txt";
string lastText = "";
if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path)) ;
if(File.Exists(path))
{
while (true)
{
var text = Clipboard.GetText();
if(lastText != text)
{
File.AppendAllText(path, text);
lastText = text;
}
Thread.Sleep(2500);
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | A-Fairooz |
