[英]How can I write to a specific location in Windows Phone 8
When I press a button, I want it to overwrite a file to a specific folder. 当我按下按钮时,我希望它将文件覆盖到特定文件夹。
I use this code: 我使用以下代码:
private void btnArial_Click(object sender, RoutedEventArgs e)
{
string cssDocument = "body{font-family:\"Arial\";}";
//I want to write file style.css to folder css inside html
string path = Package.Current.InstalledLocation.Path + "\\Html\\css\\style.css";
if (File.Exists(path))
{
StreamWriter writer = new StreamWriter(path);
writer.Write(cssDocument);
writer.Close();
}
changeStyle(new FontFamily("Arial"));
}
When I tested on emulator and actual devide, it worked properly. 当我在模拟器和实际设计上进行测试时,它可以正常工作。
But when I submit app to store, it got error - the app exits when I press that button. 但是,当我提交要存储的应用程序时,出现错误-当我按下该按钮时,应用程序退出。
The install directory (Package.Current.InstalledLocation) is a read-only location. 安装目录(Package.Current.InstalledLocation)是只读位置。 Unfortunately, due to the way that Visual Studio optimizes development-time deployment, it is set to read-write when the app is deployed from VS.
不幸的是,由于Visual Studio优化了开发时部署的方式,当从VS部署应用程序时,它设置为可读写。 That's why you see a difference in behavior after you submit the app to the store.
因此,将应用程序提交到商店后,您会发现行为有所不同。
If you need to modify a file in your install directory, you must first copy it over to a writeable location - eg. 如果需要在安装目录中修改文件,则必须首先将其复制到可写位置,例如。 your Local folder.
您的本地文件夹。
I prefer using Isolated storage in WP8 to write files and it never fails. 我更喜欢在WP8中使用隔离存储来写入文件,并且它永远不会失败。 Also you can use Windows.Storage apis.
您也可以使用Windows.Storage API。
private async void MyButton_Click(object sender, RoutedEventArgs e)
{
string cssDocument = "body{font-family:\"Arial\";}";
// using Windows.Storage
StorageFolder folder = ApplicationData.Current.LocalFolder;
folder = await folder.CreateFolderAsync("HTML", CreationCollisionOption.OpenIfExists);
folder = await folder.CreateFolderAsync("CSS", CreationCollisionOption.OpenIfExists);
StorageFile file = await folder.CreateFileAsync("style.css", CreationCollisionOption.ReplaceExisting);
using (var writer = new StreamWriter(await file.OpenStreamForWriteAsync()))
{
writer.Write(cssDocument);
}
// using using System.IO.IsolatedStorage;
using (var store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (!store.DirectoryExists("HTML/CSS"))
store.CreateDirectory("HTML/CSS");
using (var writer = new StreamWriter(store.OpenFile("HTML/CSS/style.css", FileMode.Create)))
{
writer.Write(cssDocument);
}
}
changeStyle(new FontFamily("Arial"));
}
Exactly.. Write the file in Isolated storage. 完全是。将文件写入隔离存储中。 Its easier and pretty straight forward.
它更容易,也很简单。 The files here can be accessed, viewed, modified, removed, replaced in a very clear way.
可以以非常清晰的方式访问,查看,修改,删除,替换此处的文件。 I personally prefer the Isolated Storage.
我个人更喜欢隔离存储。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.