当前位置:网站首页>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();
}
}
}
边栏推荐
- Stimulsoft Ultimate Reports 2022.3.1
- 二维激光SLAM( 使用Laser Scan Matcher )
- ROS knowledge: the structure of the rviz library librviz
- Halcon knowledge: dyn_ Usage of threshold (scratch detection)
- Halcon principle: one dimensional function_ 1D type [2]
- SQL adds the problem of duplicate table records.
- 夏日炎炎玩转新加坡:盘点室内景点和夜游好去处
- 跟循泰国国内游宣传曲MV,像本地人一样游曼谷
- 面试题:举例说一下工作中你的接口测试是怎么做的?
- Chinatown hiking: feel the strong Chinese flavor in the exotic New York
猜你喜欢
![Halcon principle: one dimensional function_ 1D type [2]](/img/54/570c6e739be1ab9caa9df805965b57.png)
Halcon principle: one dimensional function_ 1D type [2]

Ros2 knowledge (1): start practicing robots

With 32 qubits! Rigetti computing enters the UK quantum computing market

涉及第三方支付接口,怎么测?

What if the test time is not enough?

How to uninstall and reinstall gazebo

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

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

详解PyQt5信号与槽的关系
![Halcon principle: one dimensional function_ 1D type [1]](/img/ab/c0aee923fd0a9dd8a52b8cf31a6cd7.png)
Halcon principle: one dimensional function_ 1D type [1]
随机推荐
Ros2 knowledge (6): principle and practice of coordinate object TF
「开发者说」钉钉连接器+OA审批实现学校学生假勤场景数字化
华为云GaussDB重磅发布HTAP商用,定义云原生数据库2.0新范式
用户行为建模
QT知识:Qt Widgets小部件类【01】
QT knowledge: QT widgets widget function [02]
ROS2知识(1):开始实践机器人
Ablebits Ultimate Suite for Excel
Halcon principle: Auto_ Threshold operator
Transformers are RNNs (linear transformer)论文阅读
Unity学习Day14--协程和WWW
ROS察微【57】:配置手臂机器人来抓东西
2D laser Slam (using laser scan matcher)
"Developer talk" nail connector +oa approval to realize digitalization of school students' leave and work scenes
【无标题】2022年压力管道巡检维护试题及在线模拟考试
C#学习(高级课程)Day13——反射
语音模块:pyttsx变声项目
QT5知识:DNS查询
Qt5 knowledge: some key points of signals and slots
群晖万兆网络配置与测试