방법: 특정 데이터 형식의 데이터 검색

다음 예제에서는 지정된 형식으로 데이터 개체에서 데이터를 검색하는 방법을 보여 줍니다.

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

참고 항목