📎 AI Summary:
The thread discusses an issue with saving changes to a text file in a Windows app, where the user encounters an UnauthorizedAccessException when attempting to write to the installed application directory. Respondents advise that writing is restricted in that directory without elevated permissions and recommend saving settings in user-specific locations like C:\ProgramData or the app's LocalAppData folder. One contributor suggests following a guide on managing app settings using the ApplicationDataContainer.Values property for proper data storage in Windows apps.

Kenne76

New Member
Member details
Joined
Jan 3, 2016
Messages
4
Thread Author #1
Link Removed
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?
 

Solution
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

Neemobeer

Windows Forum Team
Staff member
Member details
Joined
Jul 4, 2015
Messages
8,995
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
Member details
Joined
Aug 12, 2011
Messages
161
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
 

Solution