SocketTools .NET Edition

HttpClient.GetFileSize Method (String, Int64)

Return the size of the specified file on the web server.

[Visual Basic]
Overloads Public Function GetFileSize( _
   ByVal fileName As String, _
   ByRef fileSize As Long _
) As Boolean
[C#]
public bool GetFileSize(
   string fileName,
   ref long fileSize
);

Parameters

fileName
A string that specifies a file on the server. The file name should be specified using an absolute path that begins with a leading slash character.
fileSize
A long integer passed by reference which will contain the size of the file in bytes when the method returns.

Return Value

This method returns a Boolean value. If the method succeeds, the return value is true. If the method fails, the return value is false. To get extended error information, check the value of the LastError property.

Remarks

This method uses the HEAD command to retrieve header information about the file without downloading the contents of the file itself. This requires that the server support at least version 1.0 of the protocol standard, or an error will be returned.

The server may not return a file size for some resources. This is typically the case with scripts that generate dynamic content because the server has no way of determining the size of the output generated by the script without actually executing it. The server may also not provide a file size for HTML documents which use server side includes (SSI) because that content is also dynamically created by the server. If the request to the server was successful and the file exists, but the server does not return a file size, the method will succeed but the file size returned to the caller will be zero.

When a request is made to the server for information about the file, the class library will attempt to keep the connection alive, even if the KeepAlive property has not been set to true. This allows an application to request the file size and then download the file without having to write additional code to re-establish the connection. However, it is possible that the attempt to keep the connection open will fail. In that case, an error will be returned and the session will no longer be valid. If this happens, the method may still return a valid file size. To determine if an error occurred, check the value of the LastError property.

Note that if the file on the server is a text file, it is possible that the value returned by this method will not match the size of the file when it is downloaded to the local system. This is because different operating systems use different sequences of characters to mark the end of a line of text, and when a file is transferred in text mode, the end of line character sequence is automatically converted to a carriage return-linefeed, which is the convention used by the Windows platform.

See Also

HttpClient Class | SocketTools Namespace | HttpClient.GetFileSize Overload List