我有一個需要獲取當前緩衝區文本的.NET插件。我發現這個page,這顯示了一種方法來做到這一點:Notepad ++ .NET插件 - 獲取當前緩衝區文本 - 編碼問題
public static string GetDocumentText(IntPtr curScintilla)
{
int length = (int)Win32.SendMessage(curScintilla, SciMsg.SCI_GETLENGTH, 0, 0) + 1;
StringBuilder sb = new StringBuilder(length);
Win32.SendMessage(curScintilla, SciMsg.SCI_GETTEXT, length, sb);
return sb.ToString();
}
這很好,直到我們到達的字符編碼的問題。我有一個是在編碼菜單設定爲「UTF-8無BOM」的緩衝器,並且我編寫的文本到一個文件:
System.IO.File.WriteAllText(@"C:\Users\davet\BBBBBB.txt", sb.ToString());
當我打開該文件(在記事本++)編碼菜單顯示UTF -8沒有BOM,但ß
字符被破壞(?)。
我能夠儘可能獲得儘可能找到編碼爲我當前緩衝區:
int currentBuffer = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETCURRENTBUFFERID, 0, 0);
Console.WriteLine("currentBuffer: " + currentBuffer);
int encoding = (int) Win32.SendMessage(PluginBase.nppData._nppHandle, NppMsg.NPPM_GETBUFFERENCODING, currentBuffer, 0);
Console.WriteLine("encoding = " + encoding);
而顯示爲「4」爲「UTF-8無BOM」和「0」爲「ASCII」,但我無法找到記事本++或Scintilla認爲這些值應該代表什麼。
所以我有點失落的下一步去哪裏(Windows不是我的自然棲息地)。任何人都知道我錯了什麼,或者如何進一步調試它?
謝謝。
謝謝@woddle ..完美的作品.. –