2017-05-10 129 views
4

我已经尝试了很多方法来获得一个目录,我可以保存应用程序的.exe,需要留在那里,但我尝试说每个目录都拒绝访问。我应该在哪里保存应用程序数据?

我应该为此写什么路径?当然,必须有一个管理权限不正确的路径?我确信我已经看到过这样做..

我试过了什么?

Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData) 

Path.GetTempPath() 

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文件。 –

+0

谁对恶意软件有任何评论?这也是一个个人项目,所以与恶意软件无关。 –

+0

如果你正在下载更新到你的应用程序,你应该下载到用户临时目录 – Dawid

回答

0

添加一个清单文件到你的项目需要管理员权限的可执行文件可能是一个办法解决这个问题。

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

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

相关问题