我可以在Isolated Storage Explorer中创建文件夹,但不能将文件写入该文件夹。当我使用如下代码时:
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);
}
}
仅创建文件夹,但该文件夹中没有文件。请提供在Isolated Storage Explorer中创建文件夹并将文件写入该文件夹的示例代码。这是一个WP7应用程序。
答案 0 :(得分:0)
您是否尝试直接使用isoFileStream.Write而不是使用StreamWriter对象isoFileWriter。
请使用以下代码并尝试
IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
store.CreateDirectory("JSON");
using (var isoFileStream = new IsolatedStorageFileStream("JSON\\dd.txt", FileMode.OpenOrCreate, store))
{
isoFileStream.Write(jsonFile);
}
答案 1 :(得分:0)
尝试这样的事情:
// 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);
}
}
}
此代码与我的类似,我用它将数据库从应用程序安装文件夹复制到隔离存储下的特定文件夹。希望它能为你提供一些灵感:)