如何保存文件

linxy2002 2010-05-05 09:48:51
在Silverlight 里面,如何实现点一下按钮,保存两个文件?

这两个文件不放在一个zip里,请问如何实现?
...全文
70 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sky-defender 2010-05-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jv9 的回复:]
Silverlight 4可以实现楼主的要求,Silverlight 3没有trusted in功能,如果需要保存,则要保存在独立存储空间。


C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
u……
[/Quote]

有没有Demo?
fw176170847 2010-05-06
  • 打赏
  • 举报
回复
淫荡的飘过~~~~~~~~~~~~~~~~~~~~~~~~~~~
websco 2010-05-06
  • 打赏
  • 举报
回复
冷大大终于回到地球了,欢迎

Trusted in功能 只是在限定的几个目录中能使用吧? 还是得学习学习
blackant2 2010-05-05
  • 打赏
  • 举报
回复
权限问题,sl是一部虚拟机,所以每次写客户端在理论上都需要授权
jv9 2010-05-05
  • 打赏
  • 举报
回复
Silverlight 4可以实现楼主的要求,Silverlight 3没有trusted in功能,如果需要保存,则要保存在独立存储空间。


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.IO;

namespace TrustedFileAccess
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
#region Running State/Installer Checks
// check to make sure the app is installed
// if not, only show the installer
if (App.Current.InstallState == InstallState.Installed)
{
InstallButton.Visibility = Visibility.Collapsed;
InstallWarning.Visibility = System.Windows.Visibility.Collapsed;
}
else
{
SampleArea.Visibility = System.Windows.Visibility.Collapsed;
InstallWarning.Visibility = System.Windows.Visibility.Visible;
WarningText.Text = "Application must be installed first";
return;
}

// check to make sure we're running OOB
if (!App.Current.IsRunningOutOfBrowser)
{
WarningText.Visibility = Visibility.Visible;
InstallButton.Visibility = Visibility.Collapsed;
InstallWarning.Visibility = System.Windows.Visibility.Visible;
SampleArea.Visibility = System.Windows.Visibility.Collapsed;
}
#endregion
}

private void InstallButton_Click(object sender, RoutedEventArgs e)
{
App.Current.Install();
}

private void EnumerateFiles(object sender, RoutedEventArgs e)
{
// create a collection to hold the file enumeration
List<string> videosInFolder = new List<string>();

// using the file api to enumerate
// use the SpecialFolder API to get the users low trust "My Document" type folders
var videos = Directory.EnumerateFiles(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos));

// enumerate the folder
foreach (var item in videos)
{
videosInFolder.Add(item);
}

// bind the data
VideoFileListing.ItemsSource = videosInFolder;
}

private void WriteFile(object sender, RoutedEventArgs e)
{
// create a path that we are looking to write to
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myapplog.txt");

// check to see if the file exists and delete if it does
if (File.Exists(filePath))
{
File.Delete(filePath);
}

// use a stream writer to create a file and write to the contents
StreamWriter fileWriter = File.CreateText(filePath);
fileWriter.Write(FileContents.Text);
fileWriter.Close();
}

private void ReadFile(object sender, RoutedEventArgs e)
{
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "myapplog.txt");

if (File.Exists(filePath))
{
StreamReader fileReader = File.OpenText(filePath);
string contents = fileReader.ReadToEnd();
FileContents.Text = contents;
fileReader.Close();
}
}

private void DownloadFile(object sender, RoutedEventArgs e)
{
//download some rss text
WebClient rss = new WebClient();
rss.OpenReadCompleted += new OpenReadCompletedEventHandler(rss_OpenReadCompleted);
rss.OpenReadAsync(new Uri("http://feeds.timheuer.com/timheuer"));

// get a music file
// this function commented out because the MP3 is not in the sample
// you can put your own MP3 in the folder and change the URL to download it here.
//WebClient music = new WebClient();
//music.OpenReadCompleted += new OpenReadCompletedEventHandler(music_OpenReadCompleted);
//music.OpenReadAsync(new Uri("kalimba.mp3", UriKind.RelativeOrAbsolute));
}

void music_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "music.mp3");
if (File.Exists(filePath))
{
File.Delete(filePath);
}

// create a buffer using the Stream which is the result of an OpenRead operation
byte[] buf = new byte[e.Result.Length];
e.Result.Read(buf, 0, buf.Length);
e.Result.Close();

// use the file API to write the bytes to a path.
File.WriteAllBytes(filePath, buf);
}

void rss_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "timheuerrss.xml");
if (File.Exists(filePath))
{
File.Delete(filePath);
}
byte[] buf = new byte[e.Result.Length];
e.Result.Read(buf, 0, buf.Length);
e.Result.Close();
File.WriteAllBytes(filePath, buf);
}


}
}


sky-defender 2010-05-05
  • 打赏
  • 举报
回复
无法满足你的需求,但是可以参考一下
http://blog.csdn.net/benbencoco/archive/2010/05/04/5556236.aspx

个人感觉想要实现你的这个要求比较麻烦一点
linxy2002 2010-05-05
  • 打赏
  • 举报
回复
嗯,silverlight 4 是可以的,下面有个视频教程:
http://www.silverlight.net/learn/videos/all/local-file-access

8,735

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧