Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot create file "c:\User\...\Appdata\Roaming...". Access is denied

Tags:

c#

.net

I want to store certain .txt file during app lifetime. I was thinking to use Environment.SpecialFolder.ApplicationData

so I created

 string myFilename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), address.GetHashCode() + ".txt");
...
 File.WriteAllText(myFilename, someTextContent);

I'm getting

Cannot create file "c:\User...\Appdata\Roaming...". Access is denied.

like image 230
user1765862 Avatar asked Oct 25 '25 09:10

user1765862


1 Answers

Consider using Isolated Storage, you have guaranteed read/write access with it.

Writing:

IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew, isoStore))
{
    using (StreamWriter writer = new StreamWriter(isoStream))
    {
        writer.WriteLine("Hello Isolated Storage");
    }
}

Reading:

using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("TestStore.txt", FileMode.Open, isoStore))
{
    using (StreamReader reader = new StreamReader(isoStream))
    {
        string contents = reader.ReadToEnd();
    }
}
like image 125
Saeb Amini Avatar answered Oct 27 '25 00:10

Saeb Amini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!