Silverlight learning notes-how does Silverlight read files on the client

Source: Internet
Author: User
For security reasons, we cannot read or write the local file system of the client as needed in Silverlight. In this way Program Members have security benefits.
In practice, we can present a "Open File" dialog box to the client, and the client selects the file to be opened. In this case, your Silverlight application can open the specified file (and only the specified file can be opened in this case)
This function is used in this article.
Or as in the previous Article First, we need to create a new Silverlight application. we name it readlocal filesinsl.

Page. XAML File Code The content is as follows: < Usercontrol X: Class = " Readlocal_filesinsl.page "
Xmlns = " Http://schemas.microsoft.com/winfx/2006/xaml/presentation "  
Xmlns: x = " Http://schemas.microsoft.com/winfx/2006/xaml "  
Width = " 400 " Height = " 400 " >
< Grid X: Name = " Layoutroot " Background = " Darkblue "   >
< Button X: Name = " Btnopenfile " Content = " Open " Borderthickness = " 2 " Borderbrush = " Whitesmoke " Background = " Blue " Width = " 75 " Height = " 25 " Click = " Btnopenfile_click "
Verticalalignment = " Top " Horizontalalignment = " Left " Opacity = " 1 " />
< Textblock X: Name = " Status " Margin = " 0, 25, 0, 0 " Foreground = " White " />

< Image X: Name = " Image " Minwidth = " 200 " Minheight = " 200 " Margin = " 0, 50, 0, 0 " />
</ Grid >
</ Usercontrol >

In this Code, we create a button. When we click it, the open file dialog box is displayed. A textblock control is used to display the file name of the opened file,
And an image control to display the selected image file (In this article, we only open the image file)

To read content from an object, we need to create a "Open File" dialog box and present it to the client users for their choice. Openfiledialog opfdlg =   New Openfiledialog ();
Opfdlg. multiselect =   False ; // Select only one file
Opfdlg. Filter =   " JPEG files (*. jpg) | *. jpg " ; // Only jpg image files can be selected
Bool Bresult = ( Bool ) Opfdlg. showdialog ();

In this tutorial, only JPEG image files can be opened, and only one file can be opened. Of course, we can allow users to select multiple files or multiple types of files.
Once the client selects a file, we can obtain information about the file and read its content. The Code is as follows:

System. Io. fileinfo info = Opfdlg. file;
Status. Text = Info. Name;
System. Io. Stream FS = Info. openread ();

Bitmapimage pics= NewBitmapimage ();
Pics. setsource (FS );
Image. Source=PICs;
FS. Close ();

In this example, once I open the specified file, the image will be displayed in the image control.
Of course, we can also read and process text, XML, or binary files.
The complete page. XAML code is as follows:

Code
  Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;
Using System. Windows. Media. imaging;

NamespaceReadlocal_filesinsl
{
Public Partial ClassPage: usercontrol
{
PublicPage ()
{
Initializecomponent ();
}

Private   Void Btnopenfile_click ( Object Sender, routedeventargs E)
{
Openfiledialog opfdlg =   New Openfiledialog ();
Opfdlg. multiselect =   False ; // Select only one file
Opfdlg. Filter =   " JPEG files (*. jpg) | *. jpg " ; // Only jpg image files can be selected
Bool Bresult = ( Bool ) Opfdlg. showdialog ();
If ( ! Bresult)
{
Return ;
}
Else
{
System. Io. fileinfo info = Opfdlg. file;
Status. Text = Info. Name;
System. Io. Stream FS = Info. openread ();

bitmapimage pics = New bitmapimage ();
pics. setsource (FS);
image. source = PICs;
FS. close ();
}< br>

}
}
}

Run the following command:

Go to: Silverlight Study Notes List

This article draws on some network materials and hopes to learn from them.
(For more information, see the source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.