系统路径函数应用小杂烩

本文介绍了Windows环境下如何使用API进行特殊文件夹路径的获取、临时文件的创建与操作,以及如何读取和写入文件等内容。通过具体示例展示了如何利用SHGetSpecialFolderLocation等函数获取应用程序数据路径,并演示了如何创建、读写临时文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  #include "shlobj.h" //包含头文件
WINSHELLAPI HRESULT WINAPI SHGetSpecialFolderLocation (HWND hwndOwner, int nFolder,LPITEMIDLIST * ppidl); //函数声明 用于根据nFolder的值 获得系统特殊路径,然后配合 SHGetPathFromIDList()将一个项目标识列表转换为文件系统路径:

TCHAR szAppdata[1024] = {0};

LPITEMIDLIST  ppidl;  

SHGetSpecialFolderLocation( NULL, CSIDL_COMMON_APPDATA, &ppidl);

SHGetPathFromIDList(ppidl,szAppdata);


SHGetSpecialFolderPath()功能跟上面那段代码相似,检索一个由CSIDL值指定的特殊文件夹的路径并返回。CSIDL的取值,详细情况查阅MSDN


SHGetFolderLoaction();

SHGetFolderPath();用法参见MSDN


GetTempPath();获取系统临时文件

int main()
{
HANDLE hFile;
HANDLE hTempFile; 
DWORD dwRetVal;
DWORD dwBytesRead;
DWORD dwBytesWritten; 
DWORD dwBufSize=BUFSIZE;
UINT uRetVal;
char szTempName[BUFSIZE];  
char buffer[BUFSIZE]; 
char lpPathBuffer[BUFSIZE];
BOOL fSuccess;


// Open the existing file. 
hFile = CreateFile("original.txt",        // file name 
GENERIC_READ,          // open for reading 
0,                     // do not share 
NULL,                  // default security 
OPEN_EXISTING,         // existing file only 
FILE_ATTRIBUTE_NORMAL, // normal file 
NULL);                 // no template 
if (hFile == INVALID_HANDLE_VALUE) 

printf ("CreateFile failed with error %d.\n", 
GetLastError());
return (1);



// Get the temp path.
dwRetVal = GetTempPath(dwBufSize,     // length of the buffer
lpPathBuffer); // buffer for path 
if (dwRetVal > dwBufSize || (dwRetVal == 0))
{
printf ("GetTempPath failed with error %d.\n", 
GetLastError());
return (2);
}


// Create a temporary file. 
uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files
"NEW",        // temp file name prefix 
0,            // create unique name 
szTempName);  // buffer for name 
if (uRetVal == 0)
{
printf ("GetTempFileName failed with error %d.\n", 
GetLastError());
return (3);
}


// Create the new file to write the upper-case version to.
hTempFile = CreateFile((LPTSTR) szTempName, // file name 
GENERIC_READ | GENERIC_WRITE, // open r-w 
0,                    // do not share 
NULL,                 // default security 
CREATE_ALWAYS,        // overwrite existing
FILE_ATTRIBUTE_NORMAL,// normal file 
NULL);                // no template 
if (hTempFile == INVALID_HANDLE_VALUE) 

printf ("CreateFile failed with error %d.\n", 
GetLastError());
return (4);



// Read BUFSIZE blocks to the buffer. Change all characters in 
// the buffer to upper case. Write the buffer to the temporary 
// file. 
do 
{
if (ReadFile(hFile, 
buffer, 
BUFSIZE, 
&dwBytesRead, 
NULL)) 

CharUpperBuff(buffer, dwBytesRead); 
fSuccess = WriteFile(hTempFile, 
buffer, 
dwBytesRead,
&dwBytesWritten, 
NULL); 
if (!fSuccess) 
{
printf ("WriteFile failed with error %d.\n", 
GetLastError());
return (5);
}

else
{
printf ("ReadFile failed with error %d.\n", 
GetLastError());
return (6);
}
} while (dwBytesRead == BUFSIZE); 


// Close the handles to the files.
fSuccess = CloseHandle (hFile);
if (!fSuccess) 
{
printf ("CloseHandle failed with error %d.\n", 
GetLastError());
return (7);
}
fSuccess = CloseHandle (hTempFile);
if (!fSuccess) 
{
printf ("CloseHandle failed with error %d.\n", 
GetLastError());
return (8);
}


// Move the temporary file to the new text file.
fSuccess = MoveFileEx(szTempName, 
"allcaps.txt", 
MOVEFILE_REPLACE_EXISTING);
if (!fSuccess)

printf ("MoveFileEx failed with error %d.\n", GetLastError());
return (9);
}
return (0);
}


GetModuleFileName();检索的文件的完全限定路径包含指定的模块。该模块必须在当前进程加载


LPITEMIDLIST   idl=SHBrowseForFolder(&bi);   //打开文件夹选择框
if(idl == NULL)   
{
return NULL;
}
SHGetPathFromIDList(idl,FileName);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值