当前位置:网站首页>C file download method
C file download method
2022-06-23 12:39:00 【First wife ash yuanai】
public class RemoteDownload
{
public static void DownLoad(string addressUrl,string localName)
{
// Download the file
System.NET.WebClient myWebClient = new System.Net.WebClient();
myWebClient.DownloadFile(@"/10.2.0.254/software/01279.lic.txt", "testdownload.txt");
// download end
}
}
adopt URL Get page content
try
{
// Remote access to the source code of the target page
string strTargetHtml = string.Empty;
WebClient wc = new WebClient();
wc.Credentials = CredentialCache.DefaultCredentials;
byte[] btPageData = wc.DownloadData(strTargetUrl + dtTargetDate.ToString("yyyy") + "/" + dtTargetDate.ToString("MM") + "/" + dtTargetDate.ToString("dd") + "/");
strTargetHtml = Encoding.UTF8.GetString(btPageData);
wc.Dispose();
}
catch(Exception exp)
{
_isError = true;
_errorDetail = " Error getting list of target log files :" + exp.Message;
}
adopt web The way , Download files from a remote server :
public class WebDownload
{
public static void DownLoad(string Url, string FileName)
{
bool Value = false;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
{
Value = SaveBinaryFile(response, FileName);
}
}
catch (Exception err)
{
string aa = err.ToString();
}
}
/// <summary>
/// Save a binary file to disk.
/// </summary>
/// <param name="response">The response used to save the file</param>
// Save binaries to disk
private static bool SaveBinaryFile(WebResponse response, string FileName)
{
bool Value = true;
byte[] buffer = new byte[1024];
try
{
if (File.Exists(FileName))
File.Delete(FileName);
Stream outStream = System.IO.File.Create(FileName);
Stream inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, 0, buffer.Length);
if (l > 0)
outStream.Write(buffer, 0, l);
}
while (l > 0);
outStream.Close();
inStream.Close();
}
catch
{
Value = false;
}
return Value;
}
from FTP Download files on :
public class FtpDownload
{
public static void DownLoad(string FtpPath)
{
/* First, read from the configuration file ftp Login information for */
string TempFolderPath = System.Configuration.ConfigurationManager.AppSettings["TempFolderPath"].ToString();
string FtpUserName = System.Configuration.ConfigurationManager.AppSettings["FtpUserName"].ToString();
string FtpPassWord = System.Configuration.ConfigurationManager.AppSettings["FtpPassWord"].ToString();
string LocalFileExistsOperation = System.Configuration.ConfigurationManager.AppSettings["LocalFileExistsOperation"].ToString();
Uri uri = new Uri(FtpPath);
string FileName = Path.GetFullPath(TempFolderPath) + Path.DirectorySeparatorChar.ToString() + Path.GetFileName(uri.LocalPath);
// Create a file stream
FileStream fs = null;
Stream responseStream = null;
try
{
// Create a and FTP The server contacted FtpWebRequest object
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
// The way to set the request is FTP File download
request.Method = WebRequestMethods.Ftp.DownloadFile;
// Connect login FTP The server
request.Credentials = new NetworkCredential(FtpUserName, FtpPassWord);
// Get a request response object
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// Get the response flow of the request
responseStream = response.GetResponseStream();
// Determine whether the local file exists , If there is , Then open and rewrite the local file
if (File.Exists(FileName))
{
if (LocalFileExistsOperation == "write")
{
fs = File.Open(FileName, FileMode.Open, FileAccess.ReadWrite);
}
}
// Determine whether the local file exists , If it doesn't exist , Then create a local file
else
{
fs = File.Create(FileName);
}
if (fs != null)
{
int buffer_count = 65536;
byte[] buffer = new byte[buffer_count];
int size = 0;
while ((size = responseStream.Read(buffer, 0, buffer_count)) > 0)
{
fs.Write(buffer, 0, size);
}
fs.Flush();
fs.Close();
responseStream.Close();
}
}
finally
{
if (fs != null)
fs.Close();
if (responseStream != null)
responseStream.Close();
}
}
}
边栏推荐
- 现在开户有优惠吗?手机开户安全么?
- &lt;Sicily&gt;1000. 数字反转
- M & A and capital increase may be expected to start Neusoft's new momentum?
- kubernetes comfig subpath
- DevEco Device Tool 助力OpenHarmony设备开发
- 生态 | 万里数据库与卫士通完成兼容认证 共筑网络安全生态体系
- Use xtradiagram Diagramcontrol for drawing and controlling process graphics
- What if the test time is not enough?
- 「开发者说」钉钉连接器+OA审批实现学校学生假勤场景数字化
- Ros2 knowledge (6): principle and practice of coordinate object TF
猜你喜欢

【UVM入门 ===> Episode_7 】~ sequence、sequence item、sequencer、driver

这两所985大学,共享同一位校长!校方:属实

Ablebits Ultimate Suite for Excel
![ROS observation [51]: how to integrate odometer and IMU with robots_ Localization convergence](/img/45/ea591a0c320a242a32aaecde0898bf.png)
ROS observation [51]: how to integrate odometer and IMU with robots_ Localization convergence

What are the criteria for judging the end of the test?

生态 | 万里数据库与卫士通完成兼容认证 共筑网络安全生态体系

The list of open source summer winners has been publicized, and the field of basic software has become a hot application this year

Qt5 knowledge: string list qstringlistmodel

ROS察微【51】:如何将里程计和 IMU 与 robots_localization 融合

Huawei cloud gaussdb heavily released HTAP for commercial use, defining a new paradigm of cloud native database 2.0
随机推荐
Want to learn ETS development? Teach you to develop an iq-eq test application
年薪中位数超30万,南大AI专业首届毕业生薪资曝光
ROS knowledge: the structure of the rviz library librviz
群晖万兆网络配置与测试
C#学习(高级课程)Day15——异常处理和命名空间
Ablebits Ultimate Suite for Excel
How to test the third-party payment interface?
Linked list 5 - 234 Palindrome linked list
[system architecture] - five styles of software architecture
QT知识:Qt Widgets小部件函数【02】
语音模块:pyttsx变声项目
QT5知识:QT绘制图形
halcon原理:相关性匹配
C#学习(高级课程)Day13——反射
Qt知识:视图框架QGraphicsWidget详解
ROS knowledge: the calling practice of librviz Library
DevEco Device Tool 助力OpenHarmony设备开发
Oracle database's dominant position is gradually eroded by cloud competitors
What should testers do if the requirements need to be changed when the project is half tested?
2D laser Slam (using laser scan matcher)