File Transfer sample (.NET)
Overview
This sample demostrates some of the Remoting SDK fundamentals: complex types, arrays and binary data transfer.
Getting started
- Compile the entire solution.
- Run the server application.
- Run the client application.
- Set a directory in the client's edit box and click
Go
; see the list view below populated with the selected directory contents. - Select a file to transfer and click
Download File
. Note: This sample is for illustration purposes only and contains no protective code against too large, inaccessible files and so on.
Examine the code
The server
- See the implementation of the
GetDirectory
method. Notice how the file information is packed into the array ofTFileInfo
structures, the structure is defined in the RODL.
public virtual TFileInfo[] GetDirectory(string FullPath)
{
DirectoryInfo dirinfo = new DirectoryInfo(FullPath);
// Get Directories ...
DirectoryInfo[] directories = dirinfo.GetDirectories();
// Get Files ...
FileInfo[] files = dirinfo.GetFiles("*.*");
int folderCount = directories.GetLength(0);
int fileCount = files.GetLength(0);
int itemsCount = folderCount + fileCount;
int i;
//
TFileInfo[] result = new TFileInfo[itemsCount];
for (i = 0; i < folderCount; i++)
{
result[i] = new TFileInfo();
result[i].TypeName = "Folder";
result[i].FileName = directories[i].Name;
result[i].Size = 0;
}
for (i = 0; i < fileCount; i++)
{
result[i + folderCount] = new TFileInfo();
result[i + folderCount].TypeName = files[i].Extension;
result[i + folderCount].FileName = files[i].Name;
result[i + folderCount].Size = files[i].Length;
}
return result;
}
- See the implementation of the
ReadFile
method that prepares the Binary object.
public virtual RemObjects.SDK.Types.Binary ReadFile(string FileName)
{
Binary responsedata = new Binary();
using (FileStream file = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
{
// ...
StreamHelpers.CopyFromStreamToStream(file, responsedata);
return responsedata;
}
}
The client
- Creating the service proxy that persists during the application life time:
private void Form1_Load(object sender, System.EventArgs e)
{
fFileTransfer = CoFileTransferService.Create(clientMessage, clientChannel);
}
- See how the array of structures sent by the server is consumed on the client side:
private void getFolderContents(String remotePath)
{
// ...
try
{
// ...
TFileInfo[] files = fFileTransfer.GetDirectory(remotePath);
foreach (TFileInfo file in files)
{
name = file.FileName;
size = file.Size.ToString("#,#");
typename = file.TypeName;
// ...
}
}
catch (Exception ex)
{
// Processing the exception that can happen during the remote call
MessageBox.Show("The following exception occured:\n\n" + ex.Message);
}
}
- Retrieving the binary data, i.e. the file contents:
private void bDownload_Click(object sender, System.EventArgs e)
{
// ...
try
{
// ...
Binary lResult = fFileTransfer.ReadFile(remoteFileName);
if (lResult != null)
{
// The file contents are retrieved
}
}
}
catch (Exception ex)
{
// Processing the exception that can happen during the remote call
MessageBox.Show("The following exception occured:\n\n" + ex.Message);
}
}
- Pay attention to the event handlers attached to the client channel. They allow to monitor data transfers through the channel, which is especially useful when transferring large data portions:
private void fClient_OnTransferStart(object aSender, RemObjects.SDK.TransferStartEventArgs ea)
{
// ...
}
private void fClient_OnTransferProgress(object aSender, RemObjects.SDK.TransferProgressEventArgs ea)
{
// ...
}
private void fClient_OnTransferEnd(object aSender, RemObjects.SDK.TransferEndEventArgs ea)
{
// ...
}