ChecksumExtension.GetChecksum Method

System.Net.FtpClient

Collapse image Expand Image Copy image CopyHover image
Retrieves a checksum of the given file using a checksumming method that the server supports, if any. The algorithm used goes in this order: 1. HASH command; server preferred algorithm. See FtpClient.SetHashAlgorithm() 2. MD5 / XMD5 commands 3. XSHA1 command 4. XSHA256 command 5. XSHA512 command 6. XCRC command

Namespace: System.Net.FtpClient.Extensions
Assembly: System.Net.FtpClient (in System.Net.FtpClient.dll) Version: 1.0.5064.17461

Syntax

C#
public static FtpHash GetChecksum(
	this FtpClient client,
	string path
)
Visual Basic
<ExtensionAttribute> 
Public Shared Function GetChecksum ( 
	client As FtpClient,
	path As String
) As FtpHash
Visual C++
public:
[ExtensionAttribute]
static FtpHash^ GetChecksum(
	FtpClient^ client, 
	String^ path
)

Parameters

client
Type: System.Net.FtpClient..::..FtpClient
FtpClient Object
path
Type: System..::..String
Full or relative path of the file to checksum

Return Value

Type: FtpHash
FtpHash object containing the value and algorithm. Use the IsValid property to determine if this command was successfull. FtpCommandException's can be thrown from the underlying calls.

Usage Note

In Visual Basic and C#, you can call this method as an instance method on any object of type FtpClient. When you use instance method syntax to call this method, omit the first parameter. For more information, see Extension Methods (Visual Basic) or Extension Methods (C# Programming Guide).

Examples

C#  Copy imageCopy
using System;
using System.Net;
using System.Net.FtpClient;
using System.Net.FtpClient.Extensions;

namespace Examples {
    public static class GetChecksumExample {
        public static void GetChceksumExample() {
            FtpHash hash = null;

            using (FtpClient cl = new FtpClient()) {
                cl.Credentials = new NetworkCredential("user", "pass");
                cl.Host = "some.ftpserver.on.the.internet.com";

                hash = cl.GetChecksum("/path/to/remote/file");
                // Make sure it returned a, to the best of our knowledge, valid
                // hash object. The commands for retrieving checksums are
                // non-standard extensions to the protocol so we have to
                // presume that the response was in a format understood by
                // System.Net.FtpClient and parsed correctly.
                // 
                // In addition, there is no built-in support for verifying
                // CRC hashes. You will need to write you own or use a 
                // third-party solution.
                if (hash.IsValid && hash.Algorithm != FtpHashAlgorithm.CRC) {
                    if (hash.Verify("/some/local/file")) {
                        Console.WriteLine("The checksum's match!");
                    }
                }
            }
        }
    }
}

See Also