How to: Determine if a Data Format is Present in a Data Object

The following examples show how to use the various GetDataPresent method overloads to query whether a particular data format is present in a data object.

Description

The following example code uses the GetDataPresent(String) overload to query for the presence of a particular data format by descriptor string.

Code

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of Text data in the data object, by a data format descriptor string. 
// In this overload of GetDataPresent, the method will return true both for native data formats 
// and when the data can automatically be converted to the specifed format. 

// In this case, string data is present natively, so GetDataPresent returns false. 
string textData = null;
if (dataObject.GetDataPresent(DataFormats.StringFormat))
{
    textData = dataObject.GetData(DataFormats.StringFormat) as string;
}

// In this case, the Text data in the data object can be autoconverted to  
// Unicode text, so GetDataPresent returns "true".
byte[] unicodeData = null;
if (dataObject.GetDataPresent(DataFormats.UnicodeText))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}

Description

The following example code uses the GetDataPresent(Type) overload to query for the presence of a particular data format by type.

Code

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of String data in the data object, by type.  In this overload  
// of GetDataPresent, the method will return true both for native data formats 
// and when the data can automatically be converted to the specifed format. 

// In this case, the Text data present in the data object can be autoconverted 
// to type string (also represented by DataFormats.String), so GetDataPresent returns "true".
string stringData = null;
if (dataObject.GetDataPresent(typeof(string)))
{
    stringData = dataObject.GetData(DataFormats.Text) as string;
}

Description

The following example code uses the GetDataPresent(String, Boolean) overload to query for data by descriptor string, and specifying how to treat auto-convertible data formats.

Code

DataObject dataObject = new DataObject("Some string data to store...");

// Query for the presence of Text data in the data object, by data format descriptor string, 
// and specifying whether auto-convertible data formats are acceptable.   

// In this case, Text data is present natively, so GetDataPresent returns "true".
string textData = null;
if (dataObject.GetDataPresent(DataFormats.Text, false /* Auto-convert? */))
{
    textData = dataObject.GetData(DataFormats.Text) as string;
}

// In this case, the Text data in the data object can be autoconverted to  
// Unicode text, but it is not available natively, so GetDataPresent returns "false".
byte[] unicodeData = null;
if (dataObject.GetDataPresent(DataFormats.UnicodeText, false /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}

// In this case, the Text data in the data object can be autoconverted to  
// Unicode text, so GetDataPresent returns "true".
if (dataObject.GetDataPresent(DataFormats.UnicodeText, true /* Auto-convert? */))
{
    unicodeData = dataObject.GetData(DataFormats.UnicodeText) as byte[];
}
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft