Export (0) Print
Expand All

DataObject.GetData Method (String)

Retrieves a data object in a specified format; the data format is specified by a string.

Namespace:  System.Windows
Assembly:  System.Windows (in System.Windows.dll)

public Object GetData(
	string format
)

Parameters

format
Type: System.String
A string that specifies what format to retrieve the data as. Use the constant DataFormats.FileDrop.

Return Value

Type: System.Object
A data object that has the data in the specified format, or null if the data is not available in the specified format.

Implements

IDataObject.GetData(String)

ExceptionCondition
ArgumentException

format is not equivalent to DataFormats.FileDrop.

SecurityException

Attempted access in a mode other than Drop.

The data returned for the drop target scenario is null if no file list exists. A single dropped file creates a one-item dataset. Multiple selected files create a multi-item data set. To access the data set of a non-null result, cast the result to an array of type FileInfo. Each such FileInfo represents the file information of one dropped file.

FileInfo in Silverlight has a smaller API surface than the equivalent FileInfo class in the full framework. This difference represents the security sandbox considerations for allowing access to files from a local file system. Generally speaking, Silverlight only provides access to content of local files by providing a stream, and an instance of FileInfo is only obtainable through explicit user-initiated actions. Access to FileInfo may also be influenced through a client's opt-in settings or through elevated trust. For the drop target scenario, if you want to access the file contents from a drop action in the UI, you should call OpenRead (or perhaps OpenText) on each returned FileInfo in order to access the file contents.

If you only want the file-level information and not the file contents, you can call FileInfo.Name or FileInfo.Length. For file name lists, you can instead cast the initial results to a string array instead of to a FileInfo array. Note that you cannot access the directory information from FileInfo in Silverlight in the partial trust sandbox; those APIs are marked SecurityCriticalAttribute.

If you specifically request elevated trust for an out-of-browser application, FileInfo and related classes may have more API available. For more information, see Trusted Applications.

The following example demonstrates a Drop event handler that obtains FileInfo results from the event data and its DataObject content.

This particular example is a handler on a StackPanel. The intention is that users drag a file list of images from the local computer file system onto this panel. The panel then displays each such image file by creating a new image from the stream you get from the FileInfo objects, and adding the images to the panel so that they display in the Silverlight content area. (Full logic for assuring that the files can render in Silverlight is deliberately not shown in this example. For more information about how to verify image display through image events and other techniques, see Imaging or Image.)

private void DisplayPanel_Drop(object sender, DragEventArgs e)
{
    StackPanel sp = sender as StackPanel;
    String[] dropFormatArray = e.Data.GetFormats();
    if (dropFormatArray.Contains("FileDrop"))
    {
        object dropObjectArray = e.Data.GetData("FileDrop");
        FileInfo[] fileObj = dropObjectArray as FileInfo[];
        foreach (FileInfo fi in fileObj)
        {
            FileStream dropFile = fi.OpenRead();
            try
            {
                Image image = new Image();
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(dropFile);
                image.Source = bitmapImage;
                sp.Children.Add(image);
            }
            catch (Exception ex)
            {
                 //stream errors
                 //might also want to handle ImageFailed on added images
                 //so can prompt user about format errors etc.
             }
        }
    }
}

Silverlight

Supported in: 5, 4

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Community Additions

ADD
Show:
© 2014 Microsoft