2017-05-10 171 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"); 

它是跨平臺,並處理所有必要的邏輯 爲你。

相關問題