Answered by:
How can i access a fileshare using c# to download a file automatically and read file contents ?

Question
-
User1253338400 posted
I have a files share and am wanting to do 2 separate things .
I have created a file share and can access from file explorer bt the user needs to enter username and password.
I want to automate this so that the user just clicks on the share as follows:
\\<vm ip address>\<share folder>\myfile.exe
and it automatically downloads the file, not install it.
Also I have a text file in the share directory , how can i read its contents ?
Thanks
Monday, May 24, 2021 3:59 AM
Answers
-
User-1330468790 posted
Hi robby32,
What do you mean "file share" here?
Is it an application that you create?
If you want to implement the file download functionality for a FTP server, you might need FtpWebRequest Class. Or WebClient Class if you want to download the resource with the specified URI to a local file using WebClient.DownloadFile Method.
You can refer to below codes for reference and modify them for your real scenario.
FtpWebRequest:
private void DownloadFileFTP() { string inputfilepath = @"C:\Temp\FileName.exe"; string ftphost = "xxx.xx.x.xxx"; string ftpfilepath = "/Updater/Dir1/FileName.exe"; string ftpfullpath = "ftp://" + ftphost + ftpfilepath; using (WebClient request = new WebClient()) { request.Credentials = new NetworkCredential("UserName", "Password"); byte[] fileData = request.DownloadData(ftpfullpath); using (FileStream file = File.Create(inputfilepath)) { file.Write(fileData, 0, fileData.Length); file.Close(); } } }
WebClient:
WebClient Client = new WebClient (); Client.DownloadFile("http://xxxxxx/FileName.exe", @"C:\Temp\FileName.exe");
Hope helps.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 25, 2021 2:24 AM
All replies
-
User-1330468790 posted
Hi robby32,
What do you mean "file share" here?
Is it an application that you create?
If you want to implement the file download functionality for a FTP server, you might need FtpWebRequest Class. Or WebClient Class if you want to download the resource with the specified URI to a local file using WebClient.DownloadFile Method.
You can refer to below codes for reference and modify them for your real scenario.
FtpWebRequest:
private void DownloadFileFTP() { string inputfilepath = @"C:\Temp\FileName.exe"; string ftphost = "xxx.xx.x.xxx"; string ftpfilepath = "/Updater/Dir1/FileName.exe"; string ftpfullpath = "ftp://" + ftphost + ftpfilepath; using (WebClient request = new WebClient()) { request.Credentials = new NetworkCredential("UserName", "Password"); byte[] fileData = request.DownloadData(ftpfullpath); using (FileStream file = File.Create(inputfilepath)) { file.Write(fileData, 0, fileData.Length); file.Close(); } } }
WebClient:
WebClient Client = new WebClient (); Client.DownloadFile("http://xxxxxx/FileName.exe", @"C:\Temp\FileName.exe");
Hope helps.
Best regards,
Sean
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Tuesday, May 25, 2021 2:24 AM -
User1253338400 posted
Thankyou exactly what i was looking for , i didnt get the username and password part correct. Now i know how to use it .
Tuesday, May 25, 2021 2:28 AM