只是爲了澄清事情。我是而不是試圖將字節數組轉換爲單個字符串。我試圖將字節數組轉換爲字符串數組。如何將字節數組轉換爲字符串數組?
我使用GetClipboardData
API從剪貼板獲取一些數據,然後我將數據從內存複製爲字節數組。當你複製多個文件(因此爲CF_HDROP
剪貼板格式)時,我想將此字節數組轉換爲複製文件的字符串數組。
這是我的代碼到目前爲止。
//Get pointer to clipboard data in the selected format
var clipboardDataPointer = GetClipboardData(format);
//Do a bunch of crap necessary to copy the data from the memory
//the above pointer points at to a place we can access it.
var length = GlobalSize(clipboardDataPointer);
var @lock = GlobalLock(clipboardDataPointer);
//Init a buffer which will contain the clipboard data
var buffer = new byte[(int)length];
//Copy clipboard data to buffer
Marshal.Copy(@lock, buffer, 0, (int)length);
GlobalUnlock(clipboardDataPointer);
snapshot.InsertData(format, buffer);
現在,這裏是我之後讀取緩衝區數據的代碼。
var formatter = new BinaryFormatter();
using (var serializedData = new MemoryStream(buffer))
{
paths = (string[]) formatter.Deserialize(serializedData);
}
這是行不通的,它會用一個異常說,流不包含二進制頭崩潰。我想這是因爲它不知道要反序列化的類型。
我試過看Marshal
類。沒有什麼關係。
你思考['Clipboard.GetFileDropList()'](http://msdn.microsoft.com/en-us/library/system.windows.forms.clipboard。 getfiledroplist.aspx)? – GSerg 2012-07-25 19:11:10
是的,但我想單獨使用Windows API,因爲WPF中的常規剪貼板功能有問題,正如Web上的許多地方所述。 – 2012-07-25 19:12:09