[英]C# Invalid FORMATETC structure when using GetTextAsync
I'm trying to make a progam which automatically searches for the word I copied in a file and then replaces that word in the clipboard with the line on which it was found in my file.我正在尝试制作一个程序,它会自动搜索我在文件中复制的单词,然后将剪贴板中的单词替换为在我的文件中找到的行。 I successfully setup an Eventhandler to see when the clipboard changes.
我成功地设置了一个事件处理程序来查看剪贴板何时更改。 I'm now trying to implement a way of reading my file.
我现在正在尝试实现一种读取文件的方法。
After trying to use the StringReader
the Exception is thrown:尝试使用
StringReader
后抛出异常:
Invalid FORMATETC structure occurred.
出现无效的 FORMATETC 结构。
This is my code right now:这是我现在的代码:
public partial class MainWindow : System.Windows.Window
{
string line;
string currentClipboardContent;
string expectedClipboardContent;
string vocabularygerman = Properties.Resources.vocabularygerman;
string vocabularyfrench = Properties.Resources.vocabularyfrench;
int lineNumber;
public MainWindow()
{
InitializeComponent();
Windows.ApplicationModel.DataTransfer.Clipboard.ContentChanged += new EventHandler<object>(this.TrackClipboardChanges_EventHandler);
}
private async void TrackClipboardChanges_EventHandler(object sender, object e)
{
DataPackageView dataPackageView = Windows.ApplicationModel.DataTransfer.Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Text))
{
currentClipboardContent = await dataPackageView.GetTextAsync();
if (expectedClipboardContent != currentClipboardContent)
{
Thread.Sleep(500);
using (var reader = new StringReader(vocabularygerman))
{
lineNumber = 0;
while ((line = reader.ReadLine()) != null)
{
lineNumber++;
if (line.Contains(currentClipboardContent))
{
System.Windows.Forms.Clipboard.SetDataObject(lineNumber);
break;
}
}
}
expectedClipboardContent = System.Windows.Forms.Clipboard.GetText();
}
}
}
Everything worked fine until I tried to use the StringReader.一切正常,直到我尝试使用 StringReader。 I'm thinking of ditching the stringreader altogether and using a streamreader, but I am not able to use my vocabularygerman.txt file in my resources.
我正在考虑完全放弃 stringreader 并使用 streamreader,但我无法在我的资源中使用我的 vocabularygerman.txt 文件。
StringReader
does not implement the IDataObject
interface so SetDataObject
method wont like that as it depends on that interface being present. StringReader
不实现IDataObject
接口,因此SetDataObject
方法不会喜欢它,因为它取决于该接口是否存在。
Try尝试
Clipboard.SetText(lineNumber.ToString())
instead if you need the StringReader
.相反,如果你需要
StringReader
。
PS: use await
for async
calls PS:使用
await
进行async
调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.