2017-05-10 3 views
4

私はそこにとどまる必要のあるアプリケーションの.exeを保存できるディレクトリを取得する方法をたくさん試しましたが、試したすべてのディレクトリでアクセスが拒否されたと言われています。アプリケーションデータはどこに保存しますか?

これを行うにはどのような経路を使用しますか?管理者権限が正しくないパスがある必要がありますか?私はこれが前に行われるのを見たと確信しています。

何を試しましたか?
この

Environment.GetFolderPath(
    Environment.SpecialFolder.CommonApplicationData) 

この

Path.GetTempPath() 

そしてこの

Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) 

誰もがここに助けることができますか?ここに私の完全なコードは、多分それはダウンロードとは何か関係がありますか?

通常、これらの線に沿って例外の

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. 

ラインの例外を取得

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

client.DownloadFile(downloadUrl, savePath); 
+3

任意の実行可能ファイルを簡単にダウンロードして実行しようとしている場合、マルウェアと違いはありません。 Windowsに実行可能ファイルをインストールする適切な方法は、.msiファイルです。 –

+0

誰がマルウェアについて何を言ったのですか?これは個人的なプロジェクトでもあり、マルウェアとは関係ありません。 –

+0

あなたのアプリのアップデートをダウンロードしている場合は、ユーザのtempディレクトリ – Dawid

答えて

0

あなたの実行可能ファイルの管理者権限を必要とするプロジェクトにマニフェストファイルを追加する方法かもしれないがこの問題を解決する

3

問題は...あなたがディレクトリを表現するsavePathが最初に使用していることをダウンロードしようとすると

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

...し、ファイルを表すために...

client.DownloadFile(downloadUrl, savePath); 

ですファイルが%UserProfile%\Fox\example.txtになると、example.txtが既にディレクトリとして存在する場合に指定した例外を除いて失敗します。次のスニペットは、あなたが抱えている問題は、ダウンロードしたファイルに特有のものではないことを示している:私は「

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

だけノート:

// 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連結の代わりにPath.Combineの使用をお勧めしますdは:

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

これは、クロスプラットフォームだと、必要なすべてのロジックを処理します あなたのために。

関連する問題