如何在Windows Phone的本地文件夹中保存文本文件或Excel文件

时间:2014-04-14 06:58:06

标签: c#-4.0 windows-phone-8 download

如何在Windows Phone的本地文件夹中保存文本文件或Excel文件? 我从http url获取来自Web服务响应的文本文件, 但是如何将它们保存在本地文件夹中? 有关更多想法,请参阅下面的代码。 谢谢!

Uri uri = new Uri("https://abcd/xyz/def");
WebClient wc = new WebClient();
wc.DownloadStringAsync(uri);
wc.DownloadStringCompleted += wc_DownloadStringCompleted;

void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
        string result = e.Result.ToString();

        MessageBox.Show(result);

        IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

        string FolderName = "localFolder\\myData\\";
        myIsolatedStorage.CreateDirectory(FolderName);
        string filepath = System.IO.Path.Combine(FolderName, "myfile.txt");

        using (StreamWriter write = new StreamWriter(new  IsolatedStorageFileStream(filepath, FileMode.Create, FileAccess.Write, myIsolatedStorage)))
        {
            write.WriteLine(result);
            write.Close();
        }
}

1 个答案:

答案 0 :(得分:1)

使用以下代码编写.txtFile:

 void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            String response = e.Result.ToString();
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication(); 
            //Set File Path  
             string FolderName = "localFolder\\myData\\";
             myIsolatedStorage.CreateDirectory(FolderName);
             string FilePath = System.IO.Path.Combine(FolderName, "myFile.txt"); 

            //create new file
            using (StreamWriter writeFile = new StreamWriter(new  IsolatedStorageFileStream(FilePath , FileMode.Create, FileAccess.Write, myIsolatedStorage)))
            {       
             writeFile.WriteLine(response);
             writeFile.Close();
            }
        }

检查文件是否已保存:

if (myIsolatedStorage.DirectoryExists("localFolder\\myData\\"))
{
String[] fileNames = myIsolatedStorage.GetFileNames("localFolder\\myData\\*");
}