Problem:
I built a custom web app and I wanted to download a log file directly to my downloads folder without a dialog. This app is only used by me and I specifically wanted all downloaded files in this location, no questions asked (by me, i guess)
Research:
I found many solutions that included a dialog box asking me where I want the file to be downloaded, but I wanted to bypass this process all together. The key is this line of code:
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
I figured that getting the user would be hard to do, but this pretty much takes care of it.
Solution:
I added this to my dev tools.
protected bool SaveFileToDownloadsFolder(string fullFileName, string renamedFile)
{
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, "Downloads");
try
{
WebClient Client = new WebClient();
Client.DownloadFile(fullFileName, pathDownload + @"\" + renamedFile);
}
catch (Exception)
{
//TODO: Log your error here
return false;
}
return true;
}
Hope this helps somebody.
I built a custom web app and I wanted to download a log file directly to my downloads folder without a dialog. This app is only used by me and I specifically wanted all downloaded files in this location, no questions asked (by me, i guess)
Research:
I found many solutions that included a dialog box asking me where I want the file to be downloaded, but I wanted to bypass this process all together. The key is this line of code:
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
I figured that getting the user would be hard to do, but this pretty much takes care of it.
Solution:
I added this to my dev tools.
protected bool SaveFileToDownloadsFolder(string fullFileName, string renamedFile)
{
string pathUser = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string pathDownload = Path.Combine(pathUser, "Downloads");
try
{
WebClient Client = new WebClient();
Client.DownloadFile(fullFileName, pathDownload + @"\" + renamedFile);
}
catch (Exception)
{
//TODO: Log your error here
return false;
}
return true;
}
Hope this helps somebody.
Comments
Post a Comment