Where to download file on macs using WebClient? - c#

I have an excel file I am downloading using C# and Webclient. The download works but I am wondering if it will also work on a Mac since Macs don't have a C drive.
Here is what I am using:
WebClient client = new WebClient();
client.DownloadFile(file, #"C:\" + guidToken.ToString() + ".xlsx");
The file downloads fine on windows, but if someone is using a Mac, will it work and if not, how do I get it to work?

Use Environmental variables may work.
E.g:
string documentpath = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);

Related

Downloading Files from website without knowing file name then extracting and moving to set location

I'm trying to download files using a list of urls. how would i go about downloading the files if your urls only end in the page where you would normally click the download button(it has a redirect and wait on the download also)?
i havent actually managed to get further than downloading a file using
but i know this wont work because i wont be able to know the filename and ill have to save it to a file in a config location
using (WebClient client = new WebClient())
{
client.DownloadFile("https://github.com/Hellzbellz123/downloadme/raw/master/TestAddon.7z", "testAddon.7z");
}
I intend to build a backend then plug it into a windows forms app for a gui because im really new to C# and programming in general
Do you mean that you don't know the filenames so you don't know how to save them locally?
If so:
//with 'url' as string
WebClient client = new WebClient();
Uri uri = new Uri(url);
client.DownloadFile(uri, uri.Segments.Last());
It takes the URL and splits it by every slash - the last item in the list is the filename..
EDIT: Improved, thanks to Jimi
That method won't work for links like "[..]/download.php?fileid="
For those links take a look at this

Zip file download corrupted

I have a problem with the ".zip" file download. I want to make a game launcher and I need to add the game download and install feature. When I download the packed game with ZipFile.DownloadFile from the server it gets corrupted.
This is the code:
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
// Installation folder will be set in the folder browse dialog
string installationFolder;
// I don't want to show the download url but its in Mediafire
// and I tried to download it from Google Drive
string remoteUri = "the .zip file url";
string fileName = "gamename.zip", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(remoteUri, Application.StartupPath + "/gamesdownload/" + fileName);
ZipFile.ExtractToDirectory(Application.StartupPath + "/gamesdownload/gamename.zip", installationFolder);
What is wrong with the code?
Found the answer!
I changed from Mediafire to GitHub!
And this is the solution for the error that comes:
The request was aborted: Could not create SSL/TLS secure channel

Using SSH.net in C# to get file content from a remote file

I am using SSH.NET (Renci.SshNet) library to retrieve the contents of a SQL file, but it doesnt seem to handle large files as my computer crashed when i tried to use the following code on a 500MB file.
using (SshCommand command = client.CreateCommand("cat " + filename))
{
filecontent = command.Execute();
SaveFileDialog theDialog = new SaveFileDialog();
theDialog.Title = "Backup - " + filename;
theDialog.Filter = "SQL files|*.sql";
theDialog.FileName = filename;
if (theDialog.ShowDialog() == true)
{
File.WriteAllText(theDialog.FileName, filecontent);
}
}
I then tried to use SFTP to get the file but for some reason i cannot reach the httpd.private folder so i am currently stuck when trying to copy large files.
How can i transfer large files using similar technique to my C# Application for saving? I believe that SFTP is a better solution but i cannot reach the folder the sql is generated.
The SQL file is generated through mysqldump command.
Thanks!
I had to implement the use of the SftpClient that is buildt into SSH.net and iterate over the folders using SftpClient.ListDirectory() and compare the filenames with the file i was looking for. I then opened a stream and downloaded the file using SftpClient.DownloadFile().

Download Latest File from SharePoint Server

I have very little idea on sharepoint. Need a start,how to access the share point site and download the latest file avilable in a particular folder.
I am using ASP.Net/C#.Net and Visual Studio 2008 to download file.
What is requirement means any particular libraray/update/pathes?
How can i get to know path of webservice ? (I only know the direct URL)
Is it possible to access the metadata of the file.
Any help will be appreciated.
Thanks
I found this on a web search at some point. This is what I ended up using.
var remotefile = #"https://pathtoremotefile";
var localfile = #"C:\pathtolocalfile";
using(WebClient wc = new WebClient())
{
wc.Credentials = CredentialCache.DefaultNetworkCredentials;
// or new System.Net.NetworkCredential("user","pass","domain");
wc.DownloadFile(remotefile,localfile);
}

reading a url and getting back a csv file

i have a URL and when i load it up in a browser it recognizes it as a csv file and pops up excel "do you want to open". I want to do this programatically so i can have a winforms app use that url and parse the csv file directly.
what is the quickest way to do this?
EDIT: i tried using WebClient and i am getting the folowing error:
"The remote server returned an error: (500) Internal Server Error."
I don't see why something like this wouldn't work (in C#):
// Download the file to a specified path. Using the WebClient class we can download
// files directly from a provided url, like in this case.
System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);
Where the url is your site with the csv file and the csvPath is where you want the actual file to go.
If you have a WinForms app, you can use a System.Net.WebClient to read the data as a string.
It will read the entire csv file as a string, but you can write it out or parse it at will.
If you want to just whip something together I would suggest using a scripting language and some bash. Just use wget or something similar to get the file and some scripting language to parse it. You could even use php to parse it once you had the file because I know that php has the following function which is very nice: http://php.net/manual/en/function.fgetcsv.php
I would suggest doing it this way because it is easier, this will certainly let you parse it easily enough though I don't know what you want to do with it from there but the worlds your oyster.
The following code works for me but I am running Open Office. I have not tested it with Excel.
The hacky bit is to rename the local copy of the file to *.xls so that Windows will launch Excel by default, if you leave the file extension as CSV, Windows will launch Notepad by default.
String url = "http://www.example.com/test.csv";
String localfile = "test.xls";
var client = new WebClient();
client.DownloadFile(url, localfile);
System.Diagnostics.Process.Start(localfile);

Categories