Windows 10 Can't save to text file with visual c# universal app

Kenne76

New Member

0
I can't save any changes to the text file settings.txt , but reading the file works.

The code is

private async void LedShow_Loaded(object sender, RoutedEventArgs e)
{
var path = @"settings.txt";
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
var file = await folder.GetFileAsync(path);
var lines = await Windows.Storage.FileIO.ReadLinesAsync(file);
sliderDelay.Value = Convert.ToInt32(lines[0]);
textBlockDelayValue.Text = lines[0] + " seconds";
buttonSave.IsEnabled = false;
}

and the error message is

An exception of type 'System.UnauthorizedAccessException' occurred in System.Private.CoreLib.dll but was not handled in user code
Access is denied. (Excep_FromHResult 0x80070005) occurred

Can someone help me?
 

Neemobeer

Windows Forum Team
Staff member
Yeah, you probably can't write to the program installation directory without elevated rights. To that point you shouldn't be saving settings to the program directory, either create a directory in C:\ProgramData if you want the settings to be available for all users or save into your user appdata\local\<programname> directory
 

AceInfinity

Senior Member
Microsoft MVP
Yeah, you probably can't write to the program installation directory without elevated rights. To that point you shouldn't be saving settings to the program directory, either create a directory in C:\ProgramData if you want the settings to be available for all users or save into your user appdata\local\<programname> directory
This is different though, the classes he is using are for Windows Apps.

@OP : I would suggest you follow this guide instead for app settings: Store and retrieve settings and other app data

Use the ApplicationDataContainer.Values property for simple app settings - ApplicationDataContainer.Values | values property - Windows app development
 
Top