📄 fileoperation.cpp
字号:
//FileOperation.cpp
#include "stdafx.h"
#include "FileOperation.h"
// 可以是文件名如c:\zzh.txt;也可以是目录c:\zzh
bool ddFindFile(CString filepath)
{
CFileStatus status;
if(!CFile::GetStatus(filepath, status))
return false;
return true;
}
// 得到可执行文件的完整路径(包括文件名)
CString ddGetAppFileName()
{
char path[MAX_PATH];
GetModuleFileName(NULL, path, MAX_PATH);
return path;
}
// 得到桌面的完整路径
CString ddGetDesktopDir()
{
char path[MAX_PATH];
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_DESKTOPDIRECTORY, &pidl)))
if(!SHGetPathFromIDList(pidl, path))
{
//CoUninitialize();
return "dd";
}
pShellMalloc->Free(pidl);
pShellMalloc->Release();
return path;
}
// 得到启动的完整路径
CString ddGetStartupDir()
{
char path[MAX_PATH];
LPITEMIDLIST pidl;
LPMALLOC pShellMalloc;
if(SUCCEEDED(SHGetMalloc(&pShellMalloc)))
if(SUCCEEDED(SHGetSpecialFolderLocation(NULL, CSIDL_COMMON_STARTUP, &pidl)))
if(!SHGetPathFromIDList(pidl, path))
{
//CoUninitialize();
return "dd";
}
pShellMalloc->Free(pidl);
pShellMalloc->Release();
return path;
}
// 添加快捷方式到自动启动
void ddAdd2Startup()
{
CString strShortcut, strStartup;
int iret;
strShortcut = INIFILEPATH;
strShortcut += "\\";
strShortcut += SHORTCUTNAME;
if(!ddFindFile(strShortcut))
iret = ddNewShortCut(&strShortcut);
strStartup = ddGetStartupDir();
strStartup += "\\";
strStartup += SHORTCUTNAME;
CopyFile (strShortcut, strStartup, FALSE);
}
// 从自动启动删除快捷方式
void ddDelFromStartup()
{
CString strStartup;
strStartup = ddGetStartupDir();
strStartup += "\\";
strStartup += SHORTCUTNAME;
if(!ddFindFile(strStartup))
return;
DeleteFile (strStartup);
}
//在桌面上创建快捷方式
int ddNewShortCut(CString *shortcut)
{
CString strDestDir;
strDestDir = ddGetDesktopDir();
strDestDir += "\\";
strDestDir += SHORTCUTNAME;
*shortcut = strDestDir;
if(ddFindFile(strDestDir))
return -1;
if(FAILED(CoInitialize(NULL)))
return -2;
CString strFile;
strFile = ddGetAppFileName();
IShellLink* psl;
if(SUCCEEDED(CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*) &psl)))
{
psl->SetPath(strFile);//源文件全路径
IPersistFile* ppf;
if(SUCCEEDED(psl->QueryInterface( IID_IPersistFile, (LPVOID *) &ppf)))
{
WCHAR wsz[MAX_PATH];
MultiByteToWideChar(CP_THREAD_ACP, MB_PRECOMPOSED, strDestDir,
-1, wsz, MAX_PATH);//设置快捷方式名
if(SUCCEEDED(ppf->Save(wsz, TRUE)))
TRACE("快捷方式创建成功!");
else
{
CoUninitialize();
return -3;
}
ppf->Release();
CoUninitialize();
strFile = INIFILEPATH;//此时该变量已经没有意义了,临时使用
strFile += "\\";
strFile += SHORTCUTNAME;
CopyFile (strDestDir, strFile, FALSE);
return 1;
}
psl->Release();
}
TRACE("快捷方式创建失败!");
CoUninitialize();
return -4;
}
//得到一个临时文件
CString ddNewTmpFile ()
{
TCHAR szTempPath[MAX_PATH];
TCHAR szTempFile[MAX_PATH];
DWORD dwResult = GetTempPath(MAX_PATH, szTempPath);
ASSERT(dwResult);
UINT nResult = GetTempFileName(szTempPath, _T("~ex"), 0, szTempFile);
ASSERT(nResult);
return szTempFile;
}
//新建一个ini文件
int ddNewIniFile()
{
bool bFind = true;
DWORD dwAttrs;
bFind = ddFindFile(INIFILEPATH);
if(!bFind)//若没有该目录,则创建
{
if(!::CreateDirectory(INIFILEPATH, NULL))
return -1;
dwAttrs = GetFileAttributes(INIFILEPATH);
if((dwAttrs & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN)
SetFileAttributes(INIFILEPATH, dwAttrs | FILE_ATTRIBUTE_HIDDEN);
}
bFind = ddFindFile(INIFILENAME);
if(!bFind)//若没有该文件,则创建
{
CStdioFile fileNew;
if(!fileNew.Open(INIFILENAME, CFile::modeCreate | CFile::modeReadWrite | CFile::shareDenyNone))
return -2;
dwAttrs = GetFileAttributes(INIFILENAME);
if((dwAttrs & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN)
SetFileAttributes(INIFILENAME, dwAttrs | FILE_ATTRIBUTE_HIDDEN);
fileNew.WriteString("[可用服务器]\n");
fileNew.WriteString("北京一号服务器\n");
fileNew.WriteString("北京二号服务器\n");
fileNew.WriteString("北京三号服务器\n");
fileNew.WriteString("韩国服务器\n");
fileNew.WriteString("上海服务器\n");
fileNew.WriteString("[当前服务器]\n");
fileNew.WriteString("韩国服务器\n");
fileNew.WriteString("[服务器状态]\n");
fileNew.WriteString("118/400 正常\n");
fileNew.WriteString("[开户时间]\n");
fileNew.WriteString("2008年08月08日\n");
fileNew.WriteString("[到期时间]\n");
fileNew.WriteString("2008年08月08日\n");
fileNew.WriteString("[用户名]\n");
fileNew.WriteString("vvppnn12\n");
fileNew.WriteString("[密码]\n");
fileNew.WriteString("1234\n");
fileNew.WriteString("[开机自动运行]\n");
fileNew.WriteString("0\n");
fileNew.WriteString("[自动拨号]\n");
fileNew.WriteString("0\n");
fileNew.WriteString("[保存用户名和密码]\n");
fileNew.WriteString("0");
fileNew.Close();
}
return 1;
}
//读出有关界面设置的所有信息,svList为可用服务器列表;svCur为最近使用的服务器
//srOther依次保存[服务器状态][开户时间][到期时间][用户名][密码]
//[开机自动运行][自动拨号][保存用户名和密码]
bool ddReadSettings(CStringList *svList, CString *svCur, CStringList *srOther)
{
CStdioFile file;
CString str;
POSITION pos;
BOOL bNotEof;
if(!file.Open(INIFILENAME, CFile::modeRead | CFile::shareDenyNone))
return false;
file.SeekToBegin();
bNotEof = file.ReadString(str);//[可用服务器]
bNotEof = file.ReadString(str);
while(str != "[当前服务器]")
{
pos = svList->AddTail(str);
bNotEof = file.ReadString(str);
}
bNotEof = file.ReadString(*svCur);//北京一号服务器
bNotEof = file.ReadString(str);
while(bNotEof)
{
if(str.GetAt(0) != '[')
pos = srOther->AddTail(str);
bNotEof = file.ReadString(str);
}
file.Close();
return true;
}
/*
//将文件file_path中的第line_index行替换为字符串strline
int strReplaceLineEx (char *file_path, char *tmp_file_path, char *strline, int line_index)
{
FILE *fp, *tmpfp;
char str[MAX_PATHNAME_LEN], strFilePath[MAX_PATHNAME_LEN];
int i = 0;
//GetProjectDir (strFilePath);
if((fp = fopen (file_path, "r")) == NULL)
{
MessagePopup ("请注意", "文件无法打开!");
return -1;
}
//Fmt(strFilePath, "%s<%s%s", strFilePath, "\\tmp.tmp");
//NewFilePathName(strFilePath);
if((tmpfp = fopen (tmp_file_path, "w")) == NULL)
{
MessagePopup ("请注意", "文件无法打开!");
return -2;
}
if(line_index == 1)//replace line 1
{
fprintf(tmpfp, "%s\n", strline);
LineString (fp, str);//read line 1 skip
while(!feof (fp))
{
LineString (fp, str);//read line > 1
if(!feof(fp))
fprintf(tmpfp,"%s\n", str);
else
{
fprintf(tmpfp, "%s", str);
break;
}
}
}
else//replace line > 1
{
LineString (fp, str);
fprintf(tmpfp, "%s\n", str);
while(i < line_index - 2)
{
LineString (fp, str);
fprintf(tmpfp, "%s\n", str);
i++;
}
LineString (fp, str);//read line line_index skip
if(!feof(fp))
{
fprintf(tmpfp, "%s\n", strline);
while(!feof (fp))
{
LineString (fp, str);//read line > line_index
if(!feof(fp))
fprintf(tmpfp,"%s\n", str);
else
{
fprintf(tmpfp, "%s", str);
break;
}
}
}
else
fprintf(tmpfp, "%s", strline);
}
fclose (fp);
fclose (tmpfp);
CopyFile (tmp_file_path, file_path);
return 1;
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -