Where should I save application data?

4

I have tried many ways of getting a directory where I can save an application's .exe that needs to stay there, but every single directory I try says access denied.

What path should I be writing to for this? surely there has to be a path where Admin Permission isn't right? I am sure I have seen this be done before..

What have I tried?
This

Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData)

This

Path.GetTempPath()

And this

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

Can anyone help here? here is my full code, maybe it has something to do with downloading?

string downloadUrl = "http://example.com/example.txt";
string savePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/Fox/example.txt";

if (!Directory.Exists(savePath))
{
    Directory.CreateDirectory(savePath);
}

using (var client = new WebClient())
{
    client.DownloadFile(downloadUrl, savePath);
    Process.Start(savePath);
}

Usually getting an exception along these lines

System.Net.WebException occurred
  HResult=0x80131509
  Message=An exception occurred during a WebClient request.
  Source=System
  StackTrace:
   at System.Net.WebClient.DownloadFile(Uri address, String fileName)
   at System.Net.WebClient.DownloadFile(String address, String fileName)
   at App.Program.Main(String[] args) in c:\users\user\documents\visual studio 2017\Projects\App\App\Program.cs:line 26

Inner Exception 1:
UnauthorizedAccessException: Access to the path 'C:\Users\User\App\example.txt' is denied.

Line of exception:

client.DownloadFile(downloadUrl, savePath);
c#
asked on Stack Overflow May 10, 2017 by Ash Smith

2 Answers

3

The problem is that you are using savePath first to represent a directory...

if (!Directory.Exists(savePath))
{
    Directory.CreateDirectory(savePath);
}

...and then to represent a file...

client.DownloadFile(downloadUrl, savePath);

Attempting to download a file to %UserProfile%\Fox\example.txt will fail with the exception you specified when example.txt already exists as a directory. The following snippet demonstrates that the problem you're having is not unique to file downloads:

// Build a path to a file/directory with a random name in the user's temp directory
// Does not guarantee that path does not already exist, but assume it doesn't
string path = Path.Combine(
    Path.GetTempPath(), Path.GetRandomFileName()
);
// Create a directory at that path
DirectoryInfo directory = Directory.CreateDirectory(path);

// Create a file at the same path
// Throws UnauthorizedAccessException with message "Access to the path '...' is denied."
using (FileStream stream = File.Create(path))
{
}

Consider changing your code to the following to avoid this issue:

string downloadUrl = "http://example.com/example.txt";
string saveDirectoryPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "/Fox";
string saveFilePath = saveDirectoryPath + "/example.txt";

if (!Directory.Exists(saveDirectoryPath))
{
    Directory.CreateDirectory(saveDirectoryPath);
}

using (var client = new WebClient())
{
    client.DownloadFile(downloadUrl, saveFilePath);
    Process.Start(saveFilePath);
}

Just a note, I'd recommend using Path.Combine instead of string concatenation when building paths:

string saveDirectoryPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Fox");
string saveFilePath = Path.Combine(saveDirectoryPath, "example.txt");

It's cross-platform and handles all the necessary logic for you.

answered on Stack Overflow May 10, 2017 by BACON • edited May 15, 2017 by BACON
0

Adding a manifest file to your project to require administrator privileges for your executable might be a way of solving this issue.

answered on Stack Overflow May 10, 2017 by Matt

User contributions licensed under CC BY-SA 3.0