Alan的编程小语

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

原文地址:http://www.michaelsnow.com/2010/12/28/silverlight-reading-from-a-file-contained-in-your-xap/

3步骤实现从XAP中读取文件。

1)将文件加入到Silverlight项目中,并且将文件的Build Action设置为”Resuource”

 

2)使用如下的代码,创建URI指向文件,并且从URI中建立StreamResourceInfo

Uri uri = new Uri("SilverlightApplication6;component/MyFile.txt", UriKind.Relative)
StreamResourceInfo streamInfo = Application.GetResourceStream(uri); 
其中SilverlightApplication6是silverlight程序的namespace,MyFile.txt是我们增加到Silverlight工程中的文件
 
3)从StreamResourceInfo中读取想关的Stream,然后使用StreamReader
if (null != streamInfo)
{
    Stream stream = streamInfo.Stream;
    StreamReader sr = new StreamReader(stream);
 
    string line = String.Empty;
    while ((line = sr.ReadLine()) != null)
    {
    }
}
posted on 2010-12-30 23:07  Alan.Mo  阅读(231)  评论(0)    收藏  举报