简体   繁体   中英

Clear Contents of A Text File C# (WinRT)

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.

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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM