本文整理汇总了C#中FtpClient.GetFileSize方法的典型用法代码示例。如果您正苦于以下问题:C# FtpClient.GetFileSize方法的具体用法?C# FtpClient.GetFileSize怎么用?C# FtpClient.GetFileSize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FtpClient
的用法示例。
在下文中一共展示了FtpClient.GetFileSize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetFileSize
public static void GetFileSize() {
using (FtpClient conn = new FtpClient()) {
conn.Host = "localhost";
conn.Credentials = new NetworkCredential("ftptest", "ftptest");
Console.WriteLine("The file size is: {0}",
conn.GetFileSize("/full/or/relative/path/to/file"));
}
}
示例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: GetNameListing
public static void GetNameListing() {
using (FtpClient cl = new FtpClient()) {
cl.Credentials = new NetworkCredential("ftp", "ftp");
cl.Host = "ftp.example.com";
cl.Connect();
foreach (string s in cl.GetNameListing()) {
// load some information about the object
// returned from the listing...
bool isDirectory = cl.DirectoryExists(s);
DateTime modify = cl.GetModifiedTime(s);
long size = isDirectory ? 0 : cl.GetFileSize(s);
}
}
}
示例4: UploadBackupFile
private void UploadBackupFile(FtpClient ftpClient, string ftpBackupPath, string backupFilePath)
{
Stream ftpBackupFile;
long totalBytesWritten = 0;
var backupFilename = Path.GetFileName(backupFilePath);
var ftpBackupFilePath = ftpBackupPath + "/" + backupFilename;
if (ftpClient.FileExists(ftpBackupFilePath))
{
var ftpBackupFileSize = ftpClient.GetFileSize(ftpBackupFilePath);
totalBytesWritten = ftpBackupFileSize;
ftpBackupFile = ftpClient.OpenAppend(ftpBackupPath + "/" + backupFilename, FtpDataType.Binary);
}
else
{
ftpBackupFile = ftpClient.OpenWrite(ftpBackupPath + "/" + backupFilename, FtpDataType.Binary);
}
try
{
using (var backupFile = File.OpenRead(backupFilePath))
{
int bytesRead;
var totalBytes = backupFile.Length;
var bytesWrittenLogPoint = 0.1;
var buffer = new byte[4096];
_log.Info(string.Format("Uploading {0} ({1:N0} bytes)...", backupFilename, totalBytes));
if (totalBytesWritten > 0)
backupFile.Seek(totalBytesWritten, SeekOrigin.Begin);
while ((bytesRead = backupFile.Read(buffer, 0, buffer.Length)) > 0)
{
ftpBackupFile.Write(buffer, 0, bytesRead);
totalBytesWritten += bytesRead;
var writtenBytesPercentage = ((float)totalBytesWritten / totalBytes);
if (writtenBytesPercentage >= bytesWrittenLogPoint)
{
_log.Info(
string.Format(
"Backup file upload progress: {0:P} ({1:N0} bytes) of {2}.",
writtenBytesPercentage,
totalBytesWritten,
backupFilename));
bytesWrittenLogPoint += 0.1;
}
}
}
}
finally
{
ftpBackupFile.Dispose();
}
}
示例5: Main
static void Main(string[] args)
{
// Código para testar as funcionalidades do FTP
Console.WriteLine("Informe os dados da conexão\n");
Console.Write("Servidor ............: ");
var remoteHost = Console.ReadLine();
Console.Write("Porta (default 21) ..: ");
var remotePort = Int32.Parse(Console.ReadLine());
Console.Write("Usuário .............: ");
var userName = Console.ReadLine();
Console.Write("Senha ...............: ");
var password = ConsoleHelper.ReadLine(true);
Console.WriteLine();
try
{
bool success;
var ftpClient = new FtpClient(remoteHost, remotePort, userName, password);
if ((success = ftpClient.Connect()) == true)
{
TraceHelper.WriteLine("*** Conectado!");
TraceHelper.WriteLine(string.Format("*** Diretório atual: {0}", ftpClient.GetCurrentDirectory()));
}
if (success && ((success = ftpClient.ChangeDirectory("web")) == true))
{
TraceHelper.WriteLine("*** Diretório alterado com sucesso!");
TraceHelper.WriteLine(string.Format("*** Diretório atual: {0}", ftpClient.GetCurrentDirectory()));
}
if (success)
{
var fileSize = ftpClient.GetFileSize("Sun.zip");
TraceHelper.WriteLine(string.Format("*** O arquivo Sun.zip tem {0} bytes", fileSize));
}
if (success && ((success = ftpClient.CreateDirectory("FtpClientTest")) == true))
{
TraceHelper.WriteLine("*** Diretório [FtpClientTest] criado com sucesso!");
}
if (success && ((success = ftpClient.RemoveDirectory("FtpClientTest")) == true))
{
TraceHelper.WriteLine("*** Diretório [FtpClientTest] removido com sucesso!");
}
if (success && ((success = ftpClient.ChangeToParentDirectory()) == true))
{
TraceHelper.WriteLine("*** Voltou para o diretório raiz");
TraceHelper.WriteLine(string.Format("*** Diretório atual: {0}", ftpClient.GetCurrentDirectory()));
}
if (success && ((success = ftpClient.List()) == true))
{
TraceHelper.WriteLine("*** Listagem efetuada com sucesso");
}
}
catch (Exception exception)
{
TraceHelper.WriteLine(string.Format("*** Ocorreu o seguinte erro durante a comunicação com o servidor FTP: {0}", exception.Message));
}
Console.ReadKey(true);
}