Category Archives: C#

This note is just intended to help a few who may be dealing with the WSFTPApi. If you have a license to this api, and the programmer who initially used it is no longer with you, you might just be in the same spot I was in creating new functionality with no support. This ActiveX control while it seems well written, and you can get to the documentation, is no longer being sold and has no support once your support has expired. Thus you are left to whatever you can put together from the documentation (which is quite large). Ipswitch has the documentation online here (however, the link seems to be broken), but no forums that I could find that deal with it.

So here’s a code snippet I used to test the api and get it working.

private void XferFile()
WsftpConnection remoteServer = GetFtpConnection();

 if (!remoteServer.PutFile(@"d:\temp",


 private WsftpConnection GetFtpConnection()
 // Command timeout
 _wsftpApi.CommandTimeout = 30;

 WsftpConnection toReturn = null;

 // Create connection
 toReturn = _wsftpApi.CreateConnection(
 "username", "password",
 (int) WsftpConnectionTypes.WSFTP_CT_FTP,

 if (toReturn == null)
 throw new Exception("CreateConnection returned null object.");

 return toReturn;

How to Compare Two Byte Arrays

I answered a post here with a refinement regarding comparing two byte arrays.

I am reposting here…

This post was meant to go at the end of another post named “How to Compare Two Byte Arrays” which was answered by Marcus AndrĂ©n, but it was closed. I used his code from that post to create this, and added two items discussed in the previous post:

1. different length arrays
2. buffer must be divisible by 4.

I simply check the remaining bytes using the two byte pointers.

public unsafe static bool CompareByteArrays(byte[] b1, byte[] b2)
if (b1.Length != b2.Length)
return false;
fixed (byte* bp1 = b1)
fixed (byte* bp2 = b2)
int* ip1 = (int*) bp1;
int* ip2 = (int*) bp2;
for (int i = 0; i < b1.Length/4; i++)
if (ip2[i] != ip1[i])
return false;
if ((b1.Length % 4) != 0)
for (int i = b1.Length - (b1.Length % 4); i < b1.Length; i++)
if (bp2[i] != bp1[i])
return false;

return true;



Writing BLOBs to a table in Sql Server

I have been storing large binary objects related to a database for a long time, but always ended up using a file system and storing the reference to the file (i.e. a URI, or pathname). Recently I had to actually store the object itself, and did not know where to start looking.

Since I am dealing with multi-threading and insertion of .5 gig files, I needed something that would allow me to buffer the write and not have to put the whole thing into an array before writing it. This is the solution. I know that Sql Server 2008 has a file system solution for BLOB storage and it integrates with file IO for updates, but like it seems to be so often in this career, it is too late for me to implement a new method of storage of my BLOBs. Maybe next time.

I used all sorts of search terms: binary, buffering, sql server, blob, update, insert, etc, and came up with lots of pages, but no joy until this page. It turns out that the search terms I needed were: UPDATETEXT, SCOPE_IDENTITY, and TEXTPTR. I spent a long time looking for this article and finally found and article titled:
Conserving Resources When Writing BLOB Values to SQL Server

NOTE: This was originally where this post stopped, but I have found that the command that this article uses is not going to be supported in future versions of SS, and instead we should be using UPDATE.WRITE() (available in SS2005 and beyond). The code sample above should still be helpful in integrating this new command.

Here is a blog post about UPDATE .WRITE…

Reading BLOBs in Sql Server

This post was one of the best I found on reading large binary objects from a database.
I was a bit surprised / disappointed to find that it was still writing the entire object at once in its example for writing a BLOB. So for that there is another post…
Read / Write BLOBs from / to SQL Server using C# .NET DataReader