本文整理汇总了C#中FtpClient.OpenRead方法的典型用法代码示例。如果您正苦于以下问题:C# FtpClient.OpenRead方法的具体用法?C# FtpClient.OpenRead怎么用?C# FtpClient.OpenRead使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FtpClient
的用法示例。
在下文中一共展示了FtpClient.OpenRead方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DownloadFile
public static void DownloadFile(string ftpHost, string username, string password, string fileToDownload, string filepathToWrite)
{
FtpClient chipbox = new FtpClient();
chipbox.Host = ftpHost;
chipbox.Credentials = new NetworkCredential(username, password);
Stream chipboxDosya = chipbox.OpenRead(fileToDownload, FtpDataType.Binary);
FileStream dosya = new FileStream(filepathToWrite, FileMode.Create);
try
{
int bufferSize = 8192;
int readCount;
byte[] buffer = new byte[bufferSize];
readCount = chipboxDosya.Read(buffer, 0, bufferSize);
while (readCount > 0)
{
dosya.Write(buffer, 0, readCount);
readCount = chipboxDosya.Read(buffer, 0, bufferSize);
}
}
finally
{
dosya.Close();
chipboxDosya.Close();
}
}
示例2: DownloadFile
/// <summary>
/// Get remote file. Only FTP supported at present
/// </summary>
/// <param name="file"></param>
/// <param name="destination"></param>
/// <returns></returns>
public static bool DownloadFile(Uri file, string destination)
{
try
{
string[] userInfo = file.UserInfo.Split(new[] { ':' });
if (userInfo.Length != 2)
{
Logger.Warn("No login information in URL!");
return false;
}
using (var cl = new FtpClient(userInfo[0], userInfo[1], file.Host))
{
string remoteFile = file.PathAndQuery;
// Check for existence
#warning DOESN'T SEEM TO WORK ON ALL SERVERS
// if (!cl.FileExists(remoteFile))
// return false;
long size = cl.GetFileSize(remoteFile);
using (FtpDataStream chan = cl.OpenRead(remoteFile))
{
using (var stream = new FileStream(destination, FileMode.Create))
{
using (var writer = new BinaryWriter(stream))
{
var buf = new byte[cl.ReceiveBufferSize];
int read;
long total = 0;
while ((read = chan.Read(buf, 0, buf.Length)) > 0)
{
total += read;
writer.Write(buf, 0, read);
Logger.DebugFormat("\rDownloaded: {0}/{1} {2:p2}",
total, size, (total / (double)size));
}
}
}
// when Dispose() is called on the chan object, the data channel
// stream will automatically be closed
}
// when Dispose() is called on the cl object, a logout will
// automatically be performed and the socket will be closed.
}
}
catch (Exception e)
{
Logger.Warn("Exception downloading file:" + e.Message);
return false;
}
return true;
}
示例3: DownloadBackup
private static void DownloadBackup(string host, string user, string pwd, string ftpDownloadDir)
{
using (var ftp = new FtpClient())
{
ftp.Host = host;
ftp.Credentials = new NetworkCredential { UserName = user, Password = pwd };
ftp.Connect();
foreach (var file in ftp.GetNameListing("/site/wwwroot/App_Data/Backup"))
{
using (var readStream = ftp.OpenRead(file, FtpDataType.Binary))
using (var writeStream = File.Create(Path.Combine(ftpDownloadDir, Path.GetFileName(file))))
{
readStream.CopyTo(writeStream);
}
}
}
}
示例4: OpenRead
public static void OpenRead() {
using(FtpClient conn = new FtpClient()) {
conn.Host = "localhost";
conn.Credentials = new NetworkCredential("ftptest", "ftptest");
using(Stream istream = conn.OpenRead("/full/or/relative/path/to/file")) {
try {
// istream.Position is incremented accordingly to the reads you perform
// istream.Length == file size if the server supports getting the file size
// also note that file size for the same file can vary between ASCII and Binary
// modes and some servers won't even give a file size for ASCII files! It is
// recommended that you stick with Binary and worry about character encodings
// on your end of the connection.
}
finally {
Console.WriteLine();
istream.Close();
}
}
}
}
示例5: OpenAttachment
private void OpenAttachment()
{
try
{
string strTempPath = Application.StartupPath + "/temp/";
string strFile = "";
if (dgvItems.SelectedRows.Count == 1)
{
if (!Directory.Exists(strTempPath)) Directory.CreateDirectory(strTempPath);
foreach (DataGridViewRow dr in dgvItems.SelectedRows)
{
strFile = strTempPath + dr.Cells["FileName"].Value.ToString();
if (System.IO.File.Exists(strFile))
try { System.IO.File.Delete(strFile); }
catch { }
#region Download
try
{
string strServer = "127.0.0.1";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"] != null)
{
try { strServer = System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"].ToString(); }
catch { }
}
int intPort = 21;
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"] != null)
{
try { intPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"]); }
catch { }
}
string strUserName = "ftprbsuser";
string strPassword = "ftprbspwd";
string strFTPDirectory = "attachment";
FtpClient ftpClient = new FtpClient();
ftpClient.Host = strServer;
ftpClient.Port = intPort;
ftpClient.Credentials = new NetworkCredential(strUserName, strPassword);
//IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size)
// .Where(ftpListItem => string.Equals(Path.GetExtension(ftpListItem.Name), ".dll"));
IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size);
Int32 iCount = lstFtpListItem.Count();
Int32 iCtr = 1;
// List all files with a .txt extension
foreach (FtpListItem ftpListItem in lstFtpListItem)
{
if (ftpListItem.Name.ToLower() == dr.Cells["FileName"].Value.ToString().ToLower())
{
// Report progress as a percentage of the total task.
decimal x = ((decimal.Parse(iCtr.ToString()) / decimal.Parse(iCount.ToString()) * decimal.Parse("100")) - decimal.Parse("1"));
iCtr++;
strFile = string.Format(@"{0}{1}", strTempPath, ftpListItem.Name);
using (var ftpStream = ftpClient.OpenRead(ftpListItem.FullName))
using (var fileStream = File.Create(strFile, (int)ftpStream.Length))
{
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, count);
}
}
//System.Diagnostics.Process.Start(strFile);
try
{ Common.OpenFileToDOS(strFile); }
catch (Exception ex)
{
MessageBox.Show("Error opening file. You may directly open the file @: " + strFile + Environment.NewLine + "Error details: " + ex.InnerException.Message, "RetailPlus", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error opening file, the system cannot connect to FTP server. Please consult your System Administrator." + Environment.NewLine + "Error details: " + ex.InnerException.Message, "RetailPlus", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endregion
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "RetailPlus", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
//.........这里部分代码省略.........
示例6: downloadFile
private static void downloadFile(FtpClient client, FtpListItem file, bool deleteFtpFile)
{
string destinationPath = String.Format(@"{0}\{1}", Directory.GetCurrentDirectory(), file.Name);
using (var ftpStream = client.OpenRead(file.FullName)) {
using (var fileStream = File.Create(destinationPath, (int)ftpStream.Length)) {
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0) {
fileStream.Write(buffer, 0, count);
}
}
}
DownloadedFile temp = new DownloadedFile();
temp.originalPath = file.FullName;
temp.fullOriginalPath = client.Host + file.FullName;
temp.finalPath = destinationPath;
temp.name = file.Name;
temp.extension = ".txt";
minervaFiles.Add(temp);
if (deleteFtpFile) {
client.DeleteFile(file.FullName);
}
}
示例7: getSubGroupImages
private static bool getSubGroupImages()
{
try
{
string strServer = "127.0.0.1";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"] != null)
{
try { strServer = System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"].ToString(); }
catch { }
}
int intPort = 21;
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"] != null)
{
try { intPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"]); }
catch { }
}
string strUserName = "ftprbsuser";
string strPassword = "ftprbspwd";
string strFTPDirectory = "subgroupimages";
string destinationDirectory = Application.StartupPath + @"\images\subgroups";
FtpClient ftpClient = new FtpClient();
ftpClient.Host = strServer;
ftpClient.Port = intPort;
ftpClient.Credentials = new NetworkCredential(strUserName, strPassword);
System.Collections.Generic.IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size);
// List all files with a .txt extension
foreach (FtpListItem ftpListItem in lstFtpListItem)
{
var destinationPath = string.Format(@"{0}\{1}", destinationDirectory, ftpListItem.Name);
using (var ftpStream = ftpClient.OpenRead(ftpListItem.FullName))
using (var fileStream = File.Create(destinationPath, (int)ftpStream.Length))
{
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, count);
}
}
}
}
catch
{ }
return true;
}
示例8: GetLatestVersion
private static Version GetLatestVersion()
{
Version clsVersion = new Version("0.0.0.0");
try
{
string strServer = "127.0.0.1";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"] != null)
{
try { strServer = System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"].ToString(); }
catch { }
}
int intPort = 21;
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"] != null)
{
try { intPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"]); }
catch { }
}
string strUserName = "ftprbsuser";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPUserName"] != null)
{
try { strUserName = System.Configuration.ConfigurationManager.AppSettings["VersionFTPUserName"].ToString(); }
catch { }
}
string strPassword = "ftprbspwd";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPPassword"] != null)
{
try { strPassword = System.Configuration.ConfigurationManager.AppSettings["VersionFTPPassword"].ToString(); }
catch { }
}
string strFTPDirectory = "RetailPlusClient";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPDirectory"] != null)
{
try { strFTPDirectory = System.Configuration.ConfigurationManager.AppSettings["VersionFTPDirectory"].ToString(); }
catch { }
}
string strXMLFile = Application.StartupPath + "\\version.xml";
string destinationDirectory = Application.StartupPath;
FtpClient ftpClient = new FtpClient();
ftpClient.Host = strServer;
ftpClient.Port = intPort;
ftpClient.Credentials = new NetworkCredential(strUserName, strPassword);
System.Collections.Generic.IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size);
// List all files with a .txt extension
foreach (FtpListItem ftpListItem in lstFtpListItem)
{
if (ftpListItem.Name.ToLower() == "version.xml" ||
ftpListItem.Name.ToLower() == "retailplus.versionchecker.exe" ||
ftpListItem.Name.ToLower() == "retailplus.versionchecker.exe.config")
{
var destinationPath = string.Format(@"{0}\{1}", destinationDirectory, ftpListItem.Name);
using (var ftpStream = ftpClient.OpenRead(ftpListItem.FullName))
using (var fileStream = File.Create(destinationPath, (int)ftpStream.Length))
{
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, count);
}
}
}
}
string strVersion = string.Empty;
#region Assign the Version from XML File to strVersion
XmlReader xmlReader = new XmlTextReader(strXMLFile);
xmlReader.MoveToContent();
string strElementName = string.Empty;
if ((xmlReader.NodeType == XmlNodeType.Element) && (xmlReader.Name == "RetailPlus"))
{
while (xmlReader.Read())
{
// we remember its name
if (xmlReader.NodeType == XmlNodeType.Element)
strElementName = xmlReader.Name;
else
{
// for text nodes...
if ((xmlReader.NodeType == XmlNodeType.Text) && (xmlReader.HasValue))
{
// we check what the name of the node was
switch (strElementName)
{
case "version":
strVersion = xmlReader.Value;
break;
}
}
}
//.........这里部分代码省略.........
示例9: _readXMLDocument
private XmlDocument _readXMLDocument(string documentName, FtpClient client)
{
XmlDocument xMLDoc = new XmlDocument();
if (AccessType == TDirectoryAccessType.Direct)
xMLDoc.Load(Path.Combine(_folder, documentName));
if (AccessType == TDirectoryAccessType.FTP)
{
using (Stream stream = client.OpenRead(documentName))
{
xMLDoc.Load(stream);
}
}
Debug.WriteLineIf(xMLDoc == null, string.Format("_readXMLDocument didn\'t read {0}", documentName));
return xMLDoc;
}
示例10: backgroundWorker1_DoWork
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
try
{
string strServer = "127.0.0.1";
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"] != null)
{
try { strServer = System.Configuration.ConfigurationManager.AppSettings["VersionFTPIPAddress"].ToString(); }
catch { }
}
int intPort = 21;
if (System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"] != null)
{
try { intPort = int.Parse(System.Configuration.ConfigurationManager.AppSettings["VersionFTPPort"]); }
catch { }
}
string strUserName = "ftprbsuser";
string strPassword = "ftprbspwd";
string strFTPDirectory = "retailplusclient";
string destinationDirectory = Application.StartupPath;
//string strConstantRemarks = "Please contact your system administrator immediately.";
mstStatus = "getting ftp server configuration...";
worker.ReportProgress(1);
FtpClient ftpClient = new FtpClient();
ftpClient.Host = strServer;
ftpClient.Port = intPort;
ftpClient.Credentials = new NetworkCredential(strUserName, strPassword);
mstStatus = "connecting to ftp server " + strServer + "...";
worker.ReportProgress(2);
//IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size)
// .Where(ftpListItem => string.Equals(Path.GetExtension(ftpListItem.Name), ".dll"));
IEnumerable<FtpListItem> lstFtpListItem = ftpClient.GetListing(strFTPDirectory, FtpListOption.Modify | FtpListOption.Size);
mstStatus = "connecting to ftp server " + strServer + "... done.";
worker.ReportProgress(5);
System.Threading.Thread.Sleep(10);
Int32 iCount = lstFtpListItem.Count();
Int32 iCtr = 1;
mstStatus = "copying " + iCount.ToString() + " files from retailplusclient...";
worker.ReportProgress(10);
System.Threading.Thread.Sleep(10);
// List all files with a .txt extension
foreach (FtpListItem ftpListItem in lstFtpListItem)
{
if (ftpListItem.Name.ToLower() != "version.xml" &&
ftpListItem.Name.ToLower() != "retailplus.versionchecker.exe" &
ftpListItem.Name.ToLower() != "retailplus.versionchecker.exe.config")
{
// Report progress as a percentage of the total task.
mstStatus = "copying file: " + ftpListItem.Name + " ...";
decimal x = ((decimal.Parse(iCtr.ToString()) / decimal.Parse(iCount.ToString()) * decimal.Parse("100")) - decimal.Parse("1"));
iCtr++;
Int32 iProgress = Int32.Parse(Math.Round(x, 0).ToString());
worker.ReportProgress(iProgress >= 90 ? 90 : iProgress);
var destinationPath = string.Format(@"{0}\{1}", destinationDirectory, ftpListItem.Name);
using (var ftpStream = ftpClient.OpenRead(ftpListItem.FullName))
using (var fileStream = File.Create(destinationPath, (int)ftpStream.Length))
{
var buffer = new byte[8 * 1024];
int count;
while ((count = ftpStream.Read(buffer, 0, buffer.Length)) > 0)
{
fileStream.Write(buffer, 0, count);
}
}
}
}
mstStatus = "Done copying all files...";
worker.ReportProgress(100);
System.Threading.Thread.Sleep(100);
System.Diagnostics.Process.Start(ExecutableSender);
Application.Exit();
}
catch { }
}