如何:擷取特定資料格式的資料
下列範例示範如何以指定的格式,從資料物件擷取資料。
使用 GetDataPresent (String) 多載來擷取資料
描述
下列範例程式碼會使用 GetDataPresent(String) 多載,先檢查指定的資料格式是否可用 (原生或自動轉換);如果指定的格式可用,則此範例會使用 GetData(String) 方法來擷取資料。
代碼
DataObject dataObject = new DataObject("Some string data to store...");
string desiredFormat = DataFormats.UnicodeText;
byte[] data = null;
// Use the GetDataPresent method to check for the presence of a desired data format.
// This particular overload of GetDataPresent looks for both native and auto-convertible
// data formats.
if (dataObject.GetDataPresent(desiredFormat))
{
// If the desired data format is present, use one of the GetData methods to retrieve the
// data from the data object.
data = dataObject.GetData(desiredFormat) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")
Dim desiredFormat As String = DataFormats.UnicodeText
Dim data() As Byte = Nothing
' Use the GetDataPresent method to check for the presence of a desired data format.
' This particular overload of GetDataPresent looks for both native and auto-convertible
' data formats.
If dataObject.GetDataPresent(desiredFormat) Then
' If the desired data format is present, use one of the GetData methods to retrieve the
' data from the data object.
data = TryCast(dataObject.GetData(desiredFormat), Byte())
End If
使用 GetDataPresent (String,Boolean) 多載來擷取資料
描述
下列範例程式碼會使用 GetDataPresent(String, Boolean) 多載,先檢查指定的資料格式是否可用 (過濾自動轉換的資料格式);如果指定的格式可用,則此範例會使用 GetData(String) 方法來擷取資料。
程式碼
DataObject dataObject = new DataObject("Some string data to store...");
string desiredFormat = DataFormats.UnicodeText;
bool noAutoConvert = false;
byte[] data = null;
// Use the GetDataPresent method to check for the presence of a desired data format.
// The autoconvert parameter is set to false to filter out auto-convertible data formats,
// returning true only if the specified data format is available natively.
if (dataObject.GetDataPresent(desiredFormat, noAutoConvert))
{
// If the desired data format is present, use one of the GetData methods to retrieve the
// data from the data object.
data = dataObject.GetData(desiredFormat) as byte[];
}
Dim dataObject As New DataObject("Some string data to store...")
Dim desiredFormat As String = DataFormats.UnicodeText
Dim noAutoConvert As Boolean = False
Dim data() As Byte = Nothing
' Use the GetDataPresent method to check for the presence of a desired data format.
' The autoconvert parameter is set to false to filter out auto-convertible data formats,
' returning true only if the specified data format is available natively.
If dataObject.GetDataPresent(desiredFormat, noAutoConvert) Then
' If the desired data format is present, use one of the GetData methods to retrieve the
' data from the data object.
data = TryCast(dataObject.GetData(desiredFormat), Byte())
End If