获取应用程序名及路径、写文件、服务器下载文件等小计

void GetIniFile()
{
	CString exeName = AfxGetApp()->m_pszExeName; // 获取应用程序名
	CString instPath = GetInstallPath();         
	CString iniPath;
	iniPath.Format(_T("%s\\%s.ini"), instPath, exeName);
}

CString GetInstallPath()   // 获取应用程序路径
{
	CString    sPath;   
	GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);   
	sPath.ReleaseBuffer();   
	int    nPos;   
	nPos=sPath.ReverseFind('\\');   
	sPath=sPath.Left(nPos);   
	return    sPath;  
}
void OnWrite()   // 写文件
{
	SYSTEMTIME st;
	CString strDate;
	GetLocalTime(&st);
	strDate.Format(_T("%4d-%2d-%2d  %2d:%2d:%2d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond);
	//strTime.Format("%2d:%2d:%2d",st.wHour,st.wMinute,st.wSecond);
	std::string str = CStringA(strDate);

	ofstream os(_T("E:\\123.txt"), ios::out);
	os << str.c_str() << ":  " << "wewrwerw我是中国" << endl;
	os << str.c_str() << ":  " << "13323424我是中国" << endl;
	os << "END";
	os.close();
}
void OnBnClickedBtnDownload()   //从服务器下载文件
{
	CString sPath = _T("E:\\");//下载文件的保存地址
	CString url = _T("\\\\10.1.7.29\\软件技术部\\椅旁soft\\123.txt");//下载的网址
	CString filename = sPath + (_T("1234.txt"));//下载文件的保存名
	CInternetSession session;
	CInternetFile* file = (CInternetFile*)session.OpenURL(url, 1, INTERNET_FLAG_TRANSFER_BINARY);//访问地址
	if (file == NULL)
	{
		file->Close();
		delete file;
		session.Close();
		MessageBox(_T("下载文件失败! 错误代码:1"), _T("提示"), MB_ICONERROR);
		return;
	}
	CString name = filename;
	CFile file1;
	if (file1.Open(name, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, NULL))
	{
		int readlen = -1;
		char buf[1024];
		while (1)
		{
			readlen = file->Read(buf, 1024);
			if (readlen == 0)
			{
				break;
			}
			file1.Write(buf, readlen);
			ZeroMemory(buf, 1024);
		}
		file1.Close();
	}
	else
	{
		MessageBox(_T("下载文件失败! 错误代码:2"), _T("提示"), MB_ICONERROR);
		file->Close();
		delete file;
		session.Close();
		return;
	}
	file->Close();
	delete file;
	session.Close();	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值