Where should I save application data?

I tried many ways to get a directory where I can save the .exe application, which should remain there, but every single file I try to say is prohibited.

Which way should I write for this? Of course, there should be a way when administrator permission is not suitable? I am sure I saw how this is done before.

What have i tried?
it

Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData)

it

Path.GetTempPath()

And this one

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

Can anyone help here? here is my complete code, maybe it has something to do with the download?

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 on 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.

Exclusion Line:

client.DownloadFile(downloadUrl, savePath);
+4
source share
2 answers

The problem is what you first use savePathto represent the directory ...

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

... and then submit the file ...

client.DownloadFile(downloadUrl, savePath);

%UserProfile%\Fox\example.txt , , example.txt . , , , :

// Build a path to a file/directory with a random name in the user 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))
{
}

, :

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

, Path.Combine string :

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

- .

+3

, , .

0

Source: https://habr.com/ru/post/1676867/


All Articles