Windows编程之文件/目录管理


一、文件管理
1. CreateFile
    
2. CopyFile

3. DeleteFile

4. CreateHandLink 创建硬连接
BOOL CreateHardLink(
  LPCTSTR lpFileName,                         // link name name
  LPCTSTR lpExistingFileName,                 // target file name
  LPSECURITY_ATTRIBUTES lpSecurityAttributes  
);
//经测试,该函数在现有的XP上无法使用,虽然MSDN上说可以,待解
5. MoveFile

二、目录管理
1. CreateDirectory
BOOL CreateDirectory(
  LPCTSTR lpPathName,                         // directory name
  LPSECURITY_ATTRIBUTES lpSecurityAttributes  // SD 一般设置为NULL
);//成功返回非0,失败返回0
2. SetCurrentDirectory 设置当前工作目录
BOOL SetCurrentDirectory(
  LPCTSTR lpPathName   // new directory name
);//成功返回非0,失败返回0 ; 如果目录不存在,失败
3. GetCurentDiretory 获取当前工作目录
DWORD GetCurrentDirectory(
  DWORD nBufferLength,  // size of directory buffer  [in]
  LPTSTR lpBuffer       // directory buffer [out]
);//成功则返回写入lpBuffer中的字符长度,失败返回0

三、程序实例
    下面的程序,实例如下的内容:获取当前的工作目录
                             -->创建一个目录
                             -->将新建的目录设置成当前目录
                             -->在当前路径下创建一个文件
                             -->写文件
                              -->复制文件
                              -->移动文件
/*
 * FileName: Windows_File_Management.cpp 
 * Author:   JarvisChu
 * Date:     2012-11-09
 */

#include <stdio.h>
#include <windows.h>
#include <tchar.h>   //for the useage of _T()

#define BUFF_SIZE 256

//Show a piece of Information on the Console
void ShowInfo(HANDLE hOut,TCHAR str[])
{
	DWORD nLen;
	nLen = lstrlen(str); //get the length of Info
	WriteConsole(hOut,str,nLen,NULL,NULL);
	return;
}

int main(int argc, char* argv[])
{
	
	DWORD nLen,nRead,nWrite;
	TCHAR Info[BUFF_SIZE];
	TCHAR strNewDirectory[BUFF_SIZE];
	TCHAR strDirectoryName[BUFF_SIZE];
	HANDLE hRead,hWrite,hFileOne,hFileTwo;
		
	//Get Console
	hRead = CreateFile(_T("CONIN$"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);	
	hWrite = CreateFile(_T("CONOUT$"),GENERIC_READ|GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	
	//------------------------------------------------------------------------------------------------------------------------
	//GetCurrentDirectory
	if(!(nLen = GetCurrentDirectory(BUFF_SIZE,strDirectoryName)))
	{
		wsprintf(Info,"Failed! Cannot Get Current Directory\n");// Formate the Information
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
	else
	{
		wsprintf(Info,"Success! The Current Directory is %s, length %d\n",strDirectoryName,nLen);// Formate the Information
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
	

	//------------------------------------------------------------------------------------------------------------------------
	//Create New Directory
	wsprintf(strNewDirectory,_T("C:/Jarvis"));
	if(!CreateDirectory(strNewDirectory,NULL))
	{
		wsprintf(Info,"Failed! Cannot Create the Directory named %s. Maybe it exists already\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
	else
	{
		wsprintf(Info,"Success! The Directory %s has been Created\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}

	//------------------------------------------------------------------------------------------------------------------------
	//SetCurrentDirectory	
	if (!SetCurrentDirectory(strNewDirectory))
	{
		wsprintf(Info,"Failed! Cannot set the current direactory to %s\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
	else
	{
		wsprintf(Info,"Success! The Current Directory has been changed to %s\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}

	//------------------------------------------------------------------------------------------------------------------------
	//CreateFile
	hFileOne = CreateFile(_T("FileOne.txt"),GENERIC_READ|GENERIC_WRITE,NULL,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
	if(hFileOne != INVALID_HANDLE_VALUE)
	{
		wsprintf(Info,"Success! File %s Created.\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
	else
	{
		wsprintf(Info,"Failed! Cannot Create File %s.\n",strNewDirectory); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}

	wsprintf(Info,"The Source Cpp is %s, date %s",__FILE__,__DATE__);
	nLen = lstrlen(Info);
	if(!WriteFile(hFileOne,Info,nLen,&nWrite,NULL))
	{
		wsprintf(Info,"Failed! Cannot Write File FileOne.txt.\n"); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}
    


	//-----------------------------------------------------------------------------------------------------------------------
	//CreateSymbolLink(ShortCut)
        //http://blog.csdn.net/jarvischu/article/details/5799930
	
	//-----------------------------------------------------------------------------------------------------------------------
	//CopyFile
	CloseHandle(hFileOne); //Before CopyFile, must CloseHandle hFileOne
	if(!CopyFile(_T("FileOne.txt"),_T("FileOne_Copy.txt"),FALSE))//FALSE means if the lpFileOut exists, then don't copy
	{
		wsprintf(Info,"Failed! CannotCopy File FileOne.txt.\n"); 
		ShowInfo(hWrite,Info);//Show the Info on Console
	}

	//------------------------------------------------------------------------------------------------------------------------
	//MoveFile
	MoveFile(_T("FileOne_Copy.txt"),_T("../FileOne_Copy.txt"));
 
	CloseHandle(hRead);
	CloseHandle(hWrite);
	
	return 0;
}




你可能感兴趣的:(Windows编程之文件/目录管理)