Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Trying to download file from url. code runs ok but cant see downloaded file. Help will be deeply appreciated.

What I have tried:

C#
InitializeComponent();
BindingContext = this;
var retrievefiles = new WebClient();
retrievefiles.DownloadFile("https://maliksuhail.github.io/myprices/hello.txt",System.IO.Path.Combine(FileSystem.Current.AppDataDirectory,"my.txt"));
Posted
Updated 11-Jun-23 21:09pm

Quote:
cant see downloaded file
Where did you look?

FileSystem.Current.AppDataDirectory returns different values depending on the system you are running on: Undesired behaviour of FileSystem.Current.AppDataDirectory · Issue #7657 · dotnet/maui · GitHub[^] - for some of them a Guid (presumably the App Guid) is part of the path.

So either use the debugger or a log file to find what exactly FileSystem.Current.AppDataDirectory returns on your target system and look there.
 
Share this answer
 
Comments
suhail malik 2023 12-Jun-23 2:01am    
searched the whole folder, targetting windows machine
OriginalGriff 12-Jun-23 2:44am    
Did you check for exceptions, and that the code is actually executed?
suhail malik 2023 12-Jun-23 2:51am    
yes pretty sure can see output screen.
just confirmingg, application directory is meant to be app folder or i am looking somewhere else in windows machine.
suhail malik 2023 12-Jun-23 2:57am    
i searched app folder
OriginalGriff 12-Jun-23 3:04am    
That;'s why I said "So either use the debugger or a log file to find what exactly FileSystem.Current.AppDataDirectory returns on your target system and look there."

Windows has a separate folder structure for user data - you cannot store data in the app folder as it will fail in production (the app folder in prod is under Program Files and is write protected for security reasons)

It'll be under something like "C:\Users\YourUSerName\AppData\Local\{6FADABB5-BF80-41CE-9069-6465CFBA0E7B}/my.txt" but the GUID will be app dependant.
Set the following to a variable:
C#
retrievefiles.DownloadFile("https://maliksuhail.github.io/myprices/hello.txt",System.IO.Path.Combine(FileSystem.Current.AppDataDirectory,"my.txt"));

TO:
C#
var filePath = System.IO.Path.Combine(FileSystem.Current.AppDataDirectory,"my.txt");
retrievefiles.DownloadFile("https://maliksuhail.github.io/myprices/hello.txt", filePath);

Then you can output to the debug console:
C#
Debug.WriteLine($"File Path: {filePath}");

Now you can see exactly where the file is stored.
 
Share this answer
 
v2
Comments
suhail malik 2023 12-Jun-23 5:43am    
yes it worked it was C:\Users\DELL\AppData\Local\Packages\e17ea549-673f-4fd0-a10e-1a671ecd3daf_9zz4h110yvjzm\LocalState\my.txt. i thought it would be in app directory, thanks have a nice day 😊
Dave Kreskowiak 12-Jun-23 9:07am    
Just to be clear, the "AppDataDirectory" is not "app directory", or what we would call the app directory that normally sits under Program Files, which is read only. You cannot create/write to files in any folder under Program Files.

"AppDataDirectory" is the AppData folder under the current users profile folder, usually "C:\Users\userId\AppData".
Zam Lazorde 28-Aug-23 10:00am    
this method worked for me with images but when I use DownloadFile with pdf files it doesn't work;
Graeme_Grant 29-Aug-23 18:32pm    
This method is not file-type specific. You will need to capture the error encountered and see what the issue is.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900