风云的银光志Silverlight4.0教程之遍历访问客户端用户的本地文件,该如何解决

风云的银光志Silverlight4.0教程之遍历访问客户端用户的本地文件
在Silverlight 3.0支持对用户本地文件的读写操作(OpenFileDialog和SaveFileDialog),Silverlight 4.0将允许我们访问客户端用户的本地文件,不过“本地文件”并不是指用户所有的驱动器磁盘,而是用户我的文档内的文件,其中包括:我的文档、我的音乐、我的图片、我的视频。

下面我们通过一个实例介绍如何遍历访问Silverlight客户端用户的本地文件列表,并绑定到一个ListBox控件上面显示出来。

XAML:

1 <StackPanel x:Name="LayoutRoot" Width="640" Background="White"> 
2 <StackPanel.Resources> 
3 <Style TargetType="Button"> 
4 <Setter Property="Width" Value="80"/> 
5 <Setter Property="Height" Value="30"/> 
6 <Setter Property="FontSize" Value="12"/> 
7 <Setter Property="FontFamily" Value="SimSun"/> 
8 </Style> 
9 </StackPanel.Resources> 
10 <Border Width="300" Height="20"> 
11 <TextBlock FontSize="14"> 
12 Silverlight4遍历用户本地文档 
13 </TextBlock> 
14 </Border> 
15 <StackPanel Orientation="Horizontal"> 
16 <Grid Width="100"> 
17 <Grid.RowDefinitions> 
18 <RowDefinition/> 
19 <RowDefinition/> 
20 <RowDefinition/> 
21 <RowDefinition/> 
22 </Grid.RowDefinitions> 
23 <Button x:Name="btnMyDocument" Grid.Row="0" Content="我的文档"/> 
24 <Button x:Name="btnMyVideo" Grid.Row="1" Content="我的视频"/> 
25 <Button x:Name="btnMyPhoto" Grid.Row="2" Content="我的图片"/> 
26 <Button x:Name="btnMyMusic" Grid.Row="3" Content="我的音乐"/> 
27 </Grid> 
28 <!--本地文件列表--> 
29 <ListBox x:Name="lstFiles" Width="540" Height="300"/> 
30 </StackPanel> 
31 </StackPanel> 

C#:

1 void FileSysSample_Loaded(object sender, RoutedEventArgs e) 
2 { 
3 btnMyDocument.Click += new RoutedEventHandler(btnMyDocument_Click); 
4 btnMyPhoto.Click += new RoutedEventHandler(btnMyPhoto_Click); 
5 btnMyVideo.Click += new RoutedEventHandler(btnMyVideo_Click); 
6 btnMyMusic.Click += new RoutedEventHandler(btnMyMusic_Click); 
7 } 
8  
9 void btnMyMusic_Click(object sender, RoutedEventArgs e) 
10 { 
11 folderList = new List<string>(); 
12 var musics = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic)); 
13 foreach (var item in musics) 
14 { 
15 folderList.Add(item); 
16 } 
17 lstFiles.ItemsSource = folderList; 
18 } 
19 
20 private List<string> folderList = new List<string>(); 
21 
22 void btnMyVideo_Click(object sender, RoutedEventArgs e) 
23 { 
24 folderList = new List<string>(); 
25 var videos = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos)); 
26 foreach (var item in videos)