CHttpFile (c# equivalent)

flynn

Well-known member
Joined
Jul 28, 2005
Messages
58
Can anyone point me to the C# equivalent of the C++ CHttpFile object?

Basically the C++ code just queries the size of a BINARY file that will be downloaded, sets up a progress bar and then does the download.

Here is what I am converting:
Code:
CHttpFile *pHttpFile;
CFile file;

pHttpFile = (CHttpFile*)NetSession.OpenURL(m_cstrHTTPServer + "/TestFile.exe", 1, INTERNET_FLAG_TRANSFER_BINARY | INTERNET_FLAG_RELOAD);

// get the length of the file
dwTemp = 0;
if (pHttpFile->QueryInfo(HTTP_QUERY_CONTENT_LENGTH, cstrTemp))
	dwTemp = atol(cstrTemp);

if (file.Open(m_cstrUpdatePath, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary))
{
	// copy 1KB at a time from server to local file
	UINT nRead = pHttpFile->Read(szBuff, 1023);
	while (nRead > 0)
	{
		file.Write(szBuff, nRead);

		// make sure messages are still processed during this loop
		MSG msg;
		if(::PeekMessage(&msg,m_hWnd,0,0,TRUE))
		{
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		}
		if (m_bStop) break;

		m_ctrlProgress.StepIt();

		// Add in bytes read
		ulTotalRead += nRead;

		cstrTemp.Format("Downloaded %d KB out of %d KB (%d%%).",
						ulTotalRead / 1024, dwTemp / 1024,
						(int)(((double)(ulTotalRead) / 1024.00) /
							((double)(dwTemp) / 1024.00) * 100.00));
		m_stStatus.SetWindowText(cstrTemp);

		nRead = pHttpFile->Read(szBuff, 1023);
	}
	file.Close();
}

I could use this:

Code:
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(UpdateServer + "/TestFile.exe","C:\\Data\\TestFile.bin");

which works, but doesnt allow me to update a progress bar like the C++ code. Does anyone have a C# example I could look at?

tia,
flynn
 
Back
Top