如何:以特定数据格式检索数据
更新:2007 年 11 月
下面的示例演示如何以指定的格式从数据对象中检索数据。
示例
说明
下面的示例代码使用 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[];
}
示例
说明
下面的示例代码使用 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[];
}