file.Open(_T("d:\\a.txt"),CFile::modeCreate|CFile::modeWrite)
file.Close()
比如:try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line)
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
}
您好,这样的:BOOL COscillDlg::AssoilInlaidResource(CString sDllName, UINT nResourceID, CString sOutputFileName, CString sExtendName)
{
ASSERT(!sOutputFileName.IsEmpty())
ASSERT(!sExtendName.IsEmpty())
LPTSTR Name = MAKEINTRESOURCE(nResourceID)// IDR_SWF资源名称。
HINSTANCE hImage = NULL
// 界面库句柄
// 载入界面库
if (!sDllName.IsEmpty())
{
hImage = LoadLibrary(sDllName)
if (hImage <(HINSTANCE)HINSTANCE_ERROR)
{
AfxMessageBox(sDllName)
return FALSE
}
}
HRSRC res = FindResource(hImage, Name, sExtendName)// "swf" 资源类型。
if (res == NULL)
{
AfxMessageBox(SwitchGetErr(GetLastError()))
return FALSE
}
HGLOBAL gl = LoadResource(hImage, res)
if (gl == NULL) return FALSE
LPVOID lp = LockResource(gl)//返回指向资源内存的地址的指针。
if (sOutputFileName.Find('.') == -1)// 没有找到扩展名
{
::MessageBox(NULL, TEXT("扩展名不对"), TEXT("error"), MB_OK)
return FALSE
}
DeleteFile(sOutputFileName)
// CREATE_ALWAYS为不管文件存不存在都产生新文件。
// ***CreateFile() 默认使用当前.exe文件路径创建文件。
// 创建的是一个隐藏属性的文件 FILE_ATTRIBUTE_HIDDEN
HANDLE fp = CreateFile(sOutputFileName, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_HIDDEN, NULL)
DWORD a
//sizeofResource 得到资源文件的大小
if (!WriteFile(fp, lp, SizeofResource(hImage, res), &a, NULL))
return FALSE
CloseHandle(fp)//关闭句柄
FreeResource(gl)//释放内存
if (!sDllName.IsEmpty())
{
FreeLibrary(hImage)
}
return TRUE
}
if (AssoilInlaidResource(_T("ShockwaveFlashControl.dll"), IDR_SWF1, _T("C:\\Temswf.swf"), _T("SWF")))
{
AfxMessageBox(_T("SUS"))
}
else
{
AfxMessageBox(_T("文件不存在"))
}
CString COscillDlg::SwitchGetErr(DWORD n)
{
CString errStr
switch (n)
{
case 1813:
errStr= _T("资源类型不对")
break
default:
break
}
return errStr
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)