WebClient/ Access Denied upon saving file...?
-
Hi all! :-D I was just wondering how I could elevate privelages programmatically using C# as I am trying to download a file from my site and automatically save it to My Documents on my computer but I keep getting an exception and it says access to MyDocuments denied. Does anybody know how to get around this? It happens on both my XP and Vista computers. I don't want to turn off UAC or the equivelant in XP either? I'd appreciate any advice I can get. Thanks in advance. Jay. P.S. Incase anybody wants to know how to download files using WebClient, here's the code:
WebClient DownloadClient = new WebClient();
DownloadClient.DownloadFile(StartingPointTextBox.Text, SaveSessionTextBox.Text);j.t.
-
Hi all! :-D I was just wondering how I could elevate privelages programmatically using C# as I am trying to download a file from my site and automatically save it to My Documents on my computer but I keep getting an exception and it says access to MyDocuments denied. Does anybody know how to get around this? It happens on both my XP and Vista computers. I don't want to turn off UAC or the equivelant in XP either? I'd appreciate any advice I can get. Thanks in advance. Jay. P.S. Incase anybody wants to know how to download files using WebClient, here's the code:
WebClient DownloadClient = new WebClient();
DownloadClient.DownloadFile(StartingPointTextBox.Text, SaveSessionTextBox.Text);j.t.
Hi, you don't need special privileges to write files into your own MyDocuments folder, this works just fine (tested on Vista):
string ff=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\\folder\\"; Directory.CreateDirectory(ff); File.WriteAllText(ff+"aha.txt", "contents");
Did you get an error in the path, resulting in a non-existing folder/subfolder? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, you don't need special privileges to write files into your own MyDocuments folder, this works just fine (tested on Vista):
string ff=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\\folder\\"; Directory.CreateDirectory(ff); File.WriteAllText(ff+"aha.txt", "contents");
Did you get an error in the path, resulting in a non-existing folder/subfolder? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Thanks for your reply/code :-), Here's the code that I have. I haven't forgotten any folders (I double checked after reading your message) and I'm completely baffled. I even try saving the file to the Desktop and about 15 other folders lol but it shows the same access denied error on both systems. Weird. I've never had this happen before. Below, the StartingPointTextBox is where the file's downloaded from, and the SaveSessionTextBox is where it'll be saved to:
DownloadClient.DownloadFile(StartingPointTextBox.Text, SaveSessionTextBox.Text);
In my application, I have a FolderBrowserDialog appear that lets me choose where to save the downloaded file to. Jay.
j.t.
-
Thanks for your reply/code :-), Here's the code that I have. I haven't forgotten any folders (I double checked after reading your message) and I'm completely baffled. I even try saving the file to the Desktop and about 15 other folders lol but it shows the same access denied error on both systems. Weird. I've never had this happen before. Below, the StartingPointTextBox is where the file's downloaded from, and the SaveSessionTextBox is where it'll be saved to:
DownloadClient.DownloadFile(StartingPointTextBox.Text, SaveSessionTextBox.Text);
In my application, I have a FolderBrowserDialog appear that lets me choose where to save the downloaded file to. Jay.
j.t.
Hi Jay, the code you are showing doesn't tell me anything, since the file path is to be read from a Control. AFAIK a FolderBrowserDialog returns the path to an existing folder, which is *not* the path of a file inside that folder. You can't overwrite a folder with a file. Try appending some relative file path (such as "aha.txt") and then save. FWIW: when something fails, make it log its intermediate values; as in
Console.WriteLine("filespec="+f);
That often is the fast way to spot what may be wrong. :)Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
-
Hi, you don't need special privileges to write files into your own MyDocuments folder, this works just fine (tested on Vista):
string ff=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+@"\\folder\\"; Directory.CreateDirectory(ff); File.WriteAllText(ff+"aha.txt", "contents");
Did you get an error in the path, resulting in a non-existing folder/subfolder? :)
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google - the quality and detail of your question reflects on the effectiveness of the help you are likely to get - use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
Luc Pattyn, Thank you so much for your help and code I really appreciate it. Unfortunatly, I'm a little brain dead tonight as I just figured out that I was telling it to save to a folder, but I wasn't giving it a filename to save as lol. Regards, Jay.
j.t.