简体   繁体   English

如何在Windows Phone 8中写入特定位置

[英]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.

相关问题 如何编写Windows Phone 8.1的RSS阅读器? - How can I write RSS reader for windows phone 8.1? 如何使用Windows Phone Map控件找到与当前位置最近的兴趣点? - How can I find the nearest point of intrest to my current location with the Windows Phone Map control? 如何在Windows Phone 7 XAML中检查TextBlock是否包含特定字符串? - How to write check if a TextBlock consists a specific string in windows phone 7 XAML? 如果不使用MVVM,如何为Windows(电话)8.1应用编写测试 - How can I write tests for my Windows (phone) 8.1 app if I am not using MVVM 如何使用Windows Phone 8在OBD-II适配器之间读写数据? - How can I read/write data to and from an OBD-II adapter with Windows Phone 8? 如何在项目浏览器中的“读取,写入”中访问文本文件,该文件不在Windows Phone 7的独立存储中? - How can I access a text file in the project explorer “read, write” which is not in the isolated storage in Windows Phone 7? 从另一个页面返回时如何导航到特定的中心Windows Phone 8.1 - How can I navigate to a specific hub when I come back from another page Windows Phone 8.1 如何在Windows Phone上将图像自动移动到特定的X,Y位置? - How can I move an image automatically to a specific X,Y position on windows phone? 我们如何为Windows Phone 8创建,写入和读取Excel文件 - How can we create, write and read an excel file for Windows Phone 8 在Windows Phone 8中,当我在地图上的任意位置点击时,我都会检索纬度和经度以及特定的位置名称 - in Windows Phone 8 When I Tap in any place in map i retrive Lattitude and Longitude and Specific location name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM