Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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:

 

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

 

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

 

tia,

flynn

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...