当前位置:网站首页>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();
}
}
}
边栏推荐
- Solution: argument type 'string' expected to be an instance of a class or class constrained type
- 一个 BUG 开发表示用户不会这样操作,无需修复,测试人员如何应对?
- halcon原理:一维函数function_1d类型【2】
- 冷板式、浸没式、喷淋式液冷散热能否引领高性能计算发展?
- 2022工具钳工(初级)考试练习题模拟考试平台操作
- 国产化信息 | 爱可生与中科方德完成产品兼容互认证
- Hot spot of equity transfer: 93.75% equity transfer of Chongqing Jianke Construction Engineering Quality Inspection Co., Ltd
- 语音模块:pyttsx变声项目
- A bug development means that the user will not operate like this, and there is no need to repair it. How should testers respond?
- 「开发者说」钉钉连接器+OA审批实现学校学生假勤场景数字化
猜你喜欢

夏日炎炎玩转新加坡:盘点室内景点和夜游好去处

SQL adds the problem of duplicate table records.

Oracle database's dominant position is gradually eroded by cloud competitors

首次曝光!唯一全域最高等级背后的阿里云云原生安全全景图

Qt5 knowledge: string list qstringlistmodel

冷板式、浸没式、喷淋式液冷散热能否引领高性能计算发展?

QT5知识:信号和槽的一些要点

C#部分——值类型和引用类型

Wallys/DR6018-S/ 802.11AX MU-MIMO OFDMA / 2* GE PORTS/WIFI 6e / BAND DUAL CONCURRENT

二维激光SLAM( 使用Laser Scan Matcher )
随机推荐
状态机框架
Shell process control - 39. Special process control statements
kubernetes comfig subpath
Basic data type and corresponding packing class
Use xtradiagram Diagramcontrol for drawing and controlling process graphics
Halcon principle: one dimensional function_ 1D type [1]
UI框架
面试题:举例说一下工作中你的接口测试是怎么做的?
Photon网络框架
QT5知识:信号和槽的一些要点
Interview question: for example, how do you do interface testing at work?
夏日炎炎玩转新加坡:盘点室内景点和夜游好去处
C#学习(高级课程)Day13——反射
TT-SLAM:用于平面环境的密集单目SLAM(IEEE 2021)
【基础知识】~ 数据位宽转换器
Want to learn ETS development? Teach you to develop an iq-eq test application
CRMEB知识付费如何二开阿里云短信功能
QT knowledge: detailed explanation of view frame qgraphicswidget
QT知识:Qt Widgets小部件类【01】
HMS core video editing service has the ability to open templates, helping users get the same cool video with one click