DataObject.GetData Method

Definition

Returns data in a specified data format.

Overloads

GetData(String)

Returns data in a format specified by a string.

GetData(Type)

Returns a data object in a format specified by a Type object.

GetData(String, Boolean)

Returns a data object in a specified format, optionally converting the data to the specified format.

GetData(String)

Returns data in a format specified by a string.

C#
public object GetData(string format);

Parameters

format
String

A string that specifies the format for the data. For a set of predefined data formats, see the DataFormats class.

Returns

An object that contains the data in the specified format, or null if the data is unavailable in the specified format.

Implements

Exceptions

format is null.

Examples

The following example uses this method to first check whether a specified data format is available (natively or by auto-convert); if the specified format is available, the example retrieves the data by using the GetData(String) method.

C#
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[];
}

The following example code uses the GetDataPresent(String, Boolean) method to first check if a specified data format is available natively (auto-convertible data formats are filtered); if the specified format is available, the example retrieves the data by using the GetData(String) method.

C#
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[];
}

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetData(Type)

Returns a data object in a format specified by a Type object.

C#
public object GetData(Type format);

Parameters

format
Type

A Type that specifies the format for the data. For a set of predefined data formats, see the DataFormats class.

Returns

A data object with the data in the specified format, or null if the data is unavailable in the specified format.

Implements

Exceptions

format is null.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

GetData(String, Boolean)

Returns a data object in a specified format, optionally converting the data to the specified format.

C#
public object GetData(string format, bool autoConvert);

Parameters

format
String

A string that specifies the format for the data. For a set of predefined data formats, see the DataFormats class.

autoConvert
Boolean

true to attempt to automatically convert the data to the specified format; false for no data format conversion.

Returns

A data object with the data in the specified format, or null if the data is unavailable in the specified format.

If the autoConvert parameter is true and the data cannot be converted to the specified format, or if automatic conversion is disabled (by calling SetData(String, Object, Boolean) with the autoConvert parameter set to false), this method returns null.

Implements

Exceptions

format is null.

See also

Applies to

.NET Framework 4.8.1 and other versions
Product Versions
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10