1. 程式人生 > >Windows程式設計之檔案/目錄管理

Windows程式設計之檔案/目錄管理

/*
 * 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;
}