1

I am currently working on a project using the Windows Runtime and I have run into a roadblock, this was something that was always very easy to do and I feel very frustrated for not getting this right.

I have been sitting for hours and I just cannot seem to get it right. I get the "Access is denied error", also in some variations of my code when I did click on the button, nothing happened. I feel like the answer is staring me right in the face. Here is the code:

private async void btnDtlsSaveChanges(object sender, TappedRoutedEventArgs e)
{
    StorageFile del = await Package.Current.InstalledLocation
        .GetFileAsync("UserDetails.txt");
    await del.DeleteAsync();

    StorageFile file = await Package.Current.InstalledLocation.CreateFileAsync
        ("UserDetails.txt", CreationCollisionOption.OpenIfExists);

    using (StreamWriter writer = 
        new StreamWriter(await file.OpenStreamForWriteAsync()))
    { 
        await writer.WriteLineAsync("Hello World");               
    }
}

I also tried using ReplaceExisting instead of OpenIfExists:

StorageFile file = await Package.Current.InstalledLocation.CreateFileAsync
    ("UserDetails.txt", CreationCollisionOption.ReplaceExisting);

using (StreamWriter writer = new StreamWriter(await file.OpenStreamForWriteAsync()))
{ 
    await writer.WriteLineAsync("Hello World");               
}

I have tried in several ways, all leading down the same track, and I have looked at every related question on stack overflow, nothing is getting me there.

Any help would be greatly appreciated, thanks in advance.

EDIT: (Solved) Me in my stupidity and the learning of a new technology did not actually realise that there is a difference between the LocalStorage and the actual installed location, thanks to Rob Caplan for guiding me in the right direction.

JacoJvV
  • 33
  • 7
  • shouldn't the method be decorated with the `Task` key word somethign like this for example `public static async Task btnDtlsSaveChanges` – MethodMan Nov 06 '14 at 02:01
  • 1
    You are trying to write to the install location, which is read-only. You should only write to locations such as [localfolder](http://msdn.microsoft.com/en-us/library/windows/apps/xaml/windows.storage.applicationdata.localfolder.aspx). – chue x Nov 06 '14 at 02:24
  • I'm sorry it took me a while to realize, I greatly appreciate the help guys, hopefully someone learning WinRt will stumble upon this someday and find some guidance. – JacoJvV Nov 06 '14 at 07:08
  • possible duplicate of [Access is denied when trying to CreateFileAsync in InstalledLocation StorageFolder?](http://stackoverflow.com/questions/12249734/access-is-denied-when-trying-to-createfileasync-in-installedlocation-storagefold) – chue x Nov 10 '14 at 13:08

3 Answers3

0

I don't know why you are using await, but the following code should be able to clear the file content for you

using (StreamWriter sw = new StreamWriter("UserDetails.txt", false))
{
    sw.WriteLine("Hello world");
}
Steve
  • 11,696
  • 7
  • 43
  • 81
0

You get access denied because apps don't have write access to Package.Current.InstalledLocation.

For write access you need to use your application data folders such as Windows.Storage.ApplicationData.Current.LocalFolder

If you want to ship your app with an initial data file and then update the app with user data at runtime you can copy it from InstalledLocation to LocalFolder on first run.

See App data (Windows Runtime apps) and Accessing app data with the Windows Runtime (Windows Runtime apps)

Rob Caplan - MSFT
  • 21,714
  • 3
  • 32
  • 54
  • I have used both the LocalFolder and the InstalledLocation methods, just didn't want to include all the code I tried in here, otherwise it wouldve been a very long post. – JacoJvV Nov 06 '14 at 06:18
0

I used a button for it to test

After Clicking the button the file will be empty.
You need to use using FileMode.Create, It will create a new file or overwrite it.

    private void button1_Click(object sender, EventArgs e)
    {
        using (FileStream NewFileStream = new FileStream(@"C:\Users\Crea\Documents\TCP.txt", FileMode.Create))
        { }
        //using (StreamWriter sw = new StreamWriter(@"C:\Users\Crea\Documents\TCP.txt", true))
        //    {
        //    sw.WriteLine("Hello");
        //    }
    }
Creator
  • 1,502
  • 4
  • 17
  • 30
  • The Filestream namespace is not supported in WinRT, otherwise I would have done it long ago. Thanks for the effort though. – JacoJvV Nov 06 '14 at 06:17