我应该将应用程序数据保存在哪里?

4

我已经尝试了很多方法来获取一个目录,可以保存需要留在那里的应用程序的.exe文件,但是我尝试的每个目录都显示访问被拒绝。

我应该写什么路径呢?肯定有一条路径不需要管理员权限吧?我确定以前看到过这样做的例子。

我尝试过什么?
这个。

Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData)

这是一个HTML标签。
Path.GetTempPath()

And this

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

有人可以帮忙吗?这是我的完整代码,可能与下载有关?
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);
}

通常会出现以下异常:
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.

异常的行数:

client.DownloadFile(downloadUrl, savePath);

3
如果你想轻松地下载和执行任意可执行文件,那你就和恶意软件没有什么区别。在Windows中安装可执行文件的正确方式是使用 .msi 文件。 - Guillaume CR
如果您正在下载应用程序的更新,则应将其下载到用户的临时目录。 - Dawid
访问被拒绝,因为您所做的正是恶意软件通常所做的事情,操作系统正在努力保护您的计算机安全。 - Guillaume CR
所以我想问的是,我该怎么解决这个问题?安装到下载位置然后将其复制到临时目录中? - Ash Smith
1
这不是我的问题,对吧?请保持主题。在我现在的情况下,哪个目录是可访问的? - Ash Smith
显示剩余7条评论
2个回答

3
问题在于您首先使用savePath表示目录...
if (!Directory.Exists(savePath))
{
    Directory.CreateDirectory(savePath);
}

...然后表示一个文件...

client.DownloadFile(downloadUrl, savePath);

尝试将文件下载至%UserProfile%\Fox\example.txt,当example.txt已存在且为目录时,会出现您指定的异常。以下代码片段演示了您遇到的问题不仅限于文件下载:
// 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))
{
}

请考虑修改您的代码以避免此问题: ```` 示例代码 ````
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");

它是跨平台的,能为您处理所有必要的逻辑。


0
将清单文件添加到您的项目中,以要求您的可执行文件具有管理员特权,可能是解决此问题的一种方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接