Windows phone 7 如何在windows phone 7应用程序的独立存储资源管理器中创建文件夹并将文件写入该文件夹?

Windows phone 7 如何在windows phone 7应用程序的独立存储资源管理器中创建文件夹并将文件写入该文件夹?,windows-phone-7,c#-4.0,silverlight-4.0,Windows Phone 7,C# 4.0,Silverlight 4.0,我可以在独立存储资源管理器中创建文件夹,但不能将文件写入该文件夹。当我使用如下代码时: IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication(); store.CreateDirectory("JSON"); using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, s

我可以在独立存储资源管理器中创建文件夹,但不能将文件写入该文件夹。当我使用如下代码时:

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("JSON");
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store))
{
    using (var isoFileWriter = new StreamWriter(isoFileStream))
    {
        isoFileWriter.WriteLine(jsonFile);
    }
}

仅创建文件夹,但该文件夹中没有文件。请给出在独立存储资源管理器中创建文件夹并将文件写入该文件夹的示例代码。这是一个WP7应用程序。

您是否尝试使用isoFileStream。直接写入,而不是使用StreamWriter对象isoFileWriter

请使用以下代码并重试

IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("JSON");
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store))
{
   isoFileStream.Write(jsonFile);
}

试着这样做:

    // Obtain the virtual store for the application.
    IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication();
    iso.CreateDirectory("Database");
    // Create stream for the file in the installation folder.
    using (Stream input = Application.GetResourceStream(new Uri("test.sdf", UriKind.Relative)).Stream)
    {
        // Create stream for the new file in the isolated storage
        using (IsolatedStorageFileStream output = iso.CreateFile("Database\\test.sdf"))
        {
            // Initialize the buffer
            byte[] readBuffer = new byte[4096];
            int bytesRead = -1;

            // Copy the file from installation folder to isolated storage.
            while((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0)
            {
                output.Write(readBuffer, 0, bytesRead);
            }
        }
    }
这段代码类似于我的代码,我使用它将数据库从应用程序安装文件夹复制到隔离存储下的特定文件夹。希望它能给你一些启发: