haipham23
haipham23

Reputation: 476

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.

Upvotes: 0

Views: 105

Answers (3)

Gaurav Deochakke
Gaurav Deochakke

Reputation: 2283

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.

Upvotes: 0

Sean McKenna
Sean McKenna

Reputation: 3714

The install directory (Package.Current.InstalledLocation) is a read-only location. 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. 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.

Upvotes: 1

Shivangi Gupta
Shivangi Gupta

Reputation: 876

I prefer using Isolated storage in WP8 to write files and it never fails. Also you can use Windows.Storage apis.

    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"));
    }

Upvotes: 0

Related Questions