文件操作相关示例程序
2006-08-31 15:59
323 查看
#include <dos.h>
#include <io.h>
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_readFileClick(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_delFile->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_delFileClick(TObject *Sender) // 删除文件
{
if(edt_delFile->Text.IsEmpty()) return;
if(DeleteFile(edt_delFile->Text))
MessageBox(Handle,"删除文件操作成功!","提示信息",MB_OK);
else{ //删除只读文件
_rtl_chmod(edt_delFile->Text.c_str(),1,FA_ARCH);
if(DeleteFile(edt_delFile->Text))
MessageBox(Handle,"删除文件操作成功!","提示信息",MB_OK);
else
MessageBox(Handle,"删除文件操作失败!","提示信息",MB_OK);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_delDirClick(TObject *Sender) // 删除文件夹
{
// deltree(edt_Dir->Text);
String strFolder = edt_Dir->Text;
if(strFolder.IsEmpty()) return;
if(!DirectoryExists(strFolder)){
MessageBox(Handle,"删除文件夹不存在","提示信息",MB_OK);
return;
}
String strOpt = "rd ";
strOpt += strFolder + " ";
int iResult = system(strOpt.c_str());
switch(iResult){
case ENOMEM:
MessageBox(Handle,"系统内存或资源不足!","提示信息",MB_OK);
break;
case ENOENT:
MessageBox(Handle,"路径或文件错误!","提示信息",MB_OK);
break;
case ENOEXEC:
MessageBox(Handle,"执行文件格式错误!","提示信息",MB_OK);
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_FindClick(TObject *Sender) // 搜索含隐藏文件
{
lst_ListFile->Clear();
String strFolder = edt_Folder->Text;
String strFile = edt_File->Text;
if(strFolder.IsEmpty() || strFile.IsEmpty()) return;
TSearchRec strRecord;
int iAttributes = 0;
iAttributes |= faHidden;
if(FindFirst(strFolder+"//"+strFile,iAttributes,strRecord) == 0){
if(_rtl_chmod(strRecord.Name.c_str(),0) == FA_HIDDEN) //FA_systm FA_rdonly FA_hidden FA_label FA_direc FA_arch
{
_rtl_chmod(strRecord.Name.c_str(),1,FA_ARCH);
lst_ListFile->Items->Add(strFolder+"//"+strRecord.Name);
}
while(FindNext(strRecord) == 0){
_rtl_chmod(strRecord.Name.c_str(),1,FA_ARCH);
lst_ListFile->Items->Add(strFolder+"//"+strRecord.Name);
}
FindClose(strRecord);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_FileName->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_FindStrClick(TObject *Sender) // 搜索文本文件字符串
{
TMemo *mmo_1 = new TMemo(this);
mmo_1->Parent = this;
try{
mmo_1->Lines->LoadFromFile(edt_FileName->Text);
}catch(...){
MessageBox(Handle,"文本文件不能正确打开","信息提示",MB_OK);
return;
}
int iCount = 0, i = 0;
bool bFlag = false;
char p1[255],p2[255],*p3,*p4;
for(i=0; i<mmo_1->Lines->Count; i++){
StrPCopy(p1,mmo_1->Lines->Strings[i]);
p3 = p1;
while((*p3)!= '/0'){
StrPCopy(p2,edt_String->Text);
p4 = p2;
if(strlen(p4) > strlen(p3)) break;
bFlag = true;
while((*p4)!='/0' && (*p3)!='/0'){
if( *p4 != *p3){
bFlag = false;
p3++;
break;
}
p4++;
p3++;
}
if(bFlag == true) iCount++;
}
}
if(iCount){
AnsiString strResult = "找到" + IntToStr(iCount) + "个相同的字符串";
edt_Result->Text = strResult;
}
else
edt_Result->Text = "没有找到相同的字符串";
delete mmo_1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_2Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_Long->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ShortPathClick(TObject *Sender) // 长文件名转化短文件名
{
if(edt_Long->Text.IsEmpty()) return;
char shortFileName[MAX_PATH];
GetShortPathName(edt_Long->Text.c_str(),shortFileName,MAX_PATH);
edt_Short->Text = shortFileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_MultiSelectClick(TObject *Sender) // 在文件中选择多个文件
{
lst_Multi->Clear();
dlgOpen_1->Options.Clear();
dlgOpen_1->Options<<ofAllowMultiSelect<<ofFileMustExist;
if(dlgOpen_1->Execute()){
for(int i=0; i< dlgOpen_1->Files->Count; i++)
lst_Multi->Items->Add(dlgOpen_1->Files->Strings[i]);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SystemClick(TObject *Sender) // 获得系统所有文件类型
{
lst_Multi->Clear();
HKEY hKey;
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_ENUMERATE_SUB_KEYS,&hKey)){
DWORD dwIndex = 0;
char Name[100];
SHFILEINFO shSmall;
while(RegEnumKey(hKey,dwIndex++,Name,100) == ERROR_SUCCESS){
if(Name[0] != '.') continue;
ZeroMemory(&shSmall,sizeof(shSmall));
SHGetFileInfo(Name,FILE_ATTRIBUTE_NORMAL,&shSmall,sizeof(shSmall),SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ICON | SHGFI_SMALLICON);
AnsiString strFileType = "文件扩展名: ";
strFileType += Name;
strFileType += "/t文件类型: ";
strFileType += shSmall.szTypeName;
lst_Multi->Items->Add(strFileType);
}
::RegCloseKey(hKey);
}
MessageBox(Handle,"获取系统所有文件类型操作成功","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_BigClick(TObject *Sender) // 快速创建特大型文件
{
HANDLE hFile = CreateFile(edt_Path->Text.c_str(),GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE){ // 错误处理
LPVOID strInfo;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&strInfo,0,NULL);
MessageBox(0,(LPCTSTR)strInfo,"信息提示",MB_OK|MB_ICONINFORMATION);
::LocalFree(strInfo);
return;
}
// 特大型文件大小
DWORD dwPtr = SetFilePointer(hFile,1024*1024*StrToInt(edt_Size->Text),NULL,FILE_BEGIN);
if(dwPtr == 0xFFFFFFFF){ //处理出错信息
LPVOID strInfo;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&strInfo,0,NULL);
MessageBox(0,(LPCTSTR)strInfo,"信息提示",MB_OK|MB_ICONINFORMATION);
::LocalFree(strInfo);
return;
}
SetEndOfFile(hFile);
::CloseHandle(hFile);
MessageBox(Handle,"特大型文件创建成功","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_CreateTempClick(TObject *Sender) // 产生临时文件
{
if(edt_Bf->Text.IsEmpty()) return;
char TempPath[MAX_PATH];
GetTempPath(sizeof(TempPath),TempPath);
char TempFile[MAX_PATH];
char PreFix[3];
strcpy(PreFix,edt_Bf->Text.c_str());
GetTempFileName(TempPath,PreFix,0,TempFile);
edt_TempName->Text=TempFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_5Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_File_1->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_RenameClick(TObject *Sender) // 更改文件名
{
if(edt_File_1->Text.IsEmpty() || edt_File_2->Text.IsEmpty()) return;
if(RenameFile(edt_File_1->Text,edt_File_2->Text))
MessageBox(NULL,"文件更名成功","信息提示",MB_OK);
else
MessageBox(NULL,"文件更名错误","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_3Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_FileExt->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ChangeExtClick(TObject *Sender) // 更改文件扩展名
{
if(edt_FileExt->Text.IsEmpty()) return;
String strFileName = ChangeFileExt(edt_FileExt->Text,edt_ChangeExt->Text);
if(RenameFile(edt_FileExt->Text,strFileName))
MessageBox(Handle,"文件扩展名修改操作成功","信息提示",MB_OK);
else
MessageBox(Handle,"文件扩展名修改操作失败","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_GetAttributesClick(TObject *Sender) // 获取文件属性
{
chk_Read->Checked = false;
chk_System->Checked = false;
chk_Hidden->Checked = false;
chk_Archive->Checked = false;
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
DWORD dwAttrs = GetFileAttributes(strFileName.c_str());
if(dwAttrs & FILE_ATTRIBUTE_READONLY)
chk_Read->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_SYSTEM)
chk_System->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_HIDDEN)
chk_Hidden->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_ARCHIVE)
chk_Archive->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_4Click(TObject *Sender)
{
if(dlgOpen_1->Execute()){
String fileName = dlgOpen_1->FileName;
edt->Text = fileName;
btn_GetAttributesClick(Sender); // 获取文件属性
btn_GetTimeClick(Sender); // 获取文件时间属性
edt_FilePath->Text = ExpandFileName(fileName);
edt_Ext->Text = ExtractFileExt(fileName);
edt_Name->Text = ExtractFileName(fileName);
edt_PathName->Text = ExtractFilePath(fileName);
edt_View->Text = ExtractFileDir(fileName);
edt_Drive->Text = ExtractFileDrive(fileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SetAttributesClick(TObject *Sender) // 设置文件属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
DWORD dwAttrs = GetFileAttributes(strFileName.c_str());
SetFileAttributes(strFileName.c_str(),0);
if(chk_Read->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_READONLY);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_System->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_SYSTEM);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_Hidden->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_HIDDEN);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_Archive->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_ARCHIVE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_GetTimeClick(TObject *Sender) // 获取文件时间属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
int FileHandle = FileOpen(strFileName,fmOpenRead);
if(FileHandle == -1){
MessageBox(Handle,"文件打开不正确","信息提示",MB_OK);
return;
}
TFileTime tCreate,tAccess,tWrite;
if(GetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite)){
TSystemTime SystemTime;
FileTimeToSystemTime(&tCreate,&SystemTime);
edt_CreateTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
FileTimeToSystemTime(&tAccess,&SystemTime);
edt_AccessTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
FileTimeToSystemTime(&tWrite,&SystemTime);
edt_WriteTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
}
FileClose(FileHandle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SetTimeClick(TObject *Sender) // 设置文件时间属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
int FileHandle = FileOpen(strFileName,fmOpenRead);
if(FileHandle == -1){
MessageBox(Handle,"文件打开不正确","信息提示",MB_OK);
return;
}
TFileTime tCreate,tAccess,tWrite;
if(!GetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite))
MessageBox(Handle,"获取文件时间属性操作失败","信息提示",MB_OK);
TSystemTime SystemTime;
String Temp = edt_CreateTime->Text;
SystemTime.wYear=Temp.SubString(1,4).ToInt();
SystemTime.wMonth=Temp.SubString(6,2).ToInt();
SystemTime.wDay= Temp.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tCreate);
String Temp2 = edt_AccessTime->Text;
SystemTime.wYear=Temp2.SubString(1,4).ToInt();
SystemTime.wMonth=Temp2.SubString(6,2).ToInt();
SystemTime.wDay=Temp2.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tAccess);
String Temp3 = edt_WriteTime->Text;
SystemTime.wYear=Temp3.SubString(1,4).ToInt();
SystemTime.wMonth=Temp3.SubString(6,2).ToInt();
SystemTime.wDay=Temp3.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tWrite);
if(SetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite))
MessageBox(Handle,"设置文件时间属性操作成功","信息提示",MB_OK);
else
MessageBox(Handle,"设置文件时间属性操作错误","信息提示",MB_OK);
FileClose(FileHandle);
}
#include <io.h>
#include <vcl.h>
#pragma hdrstop
#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_readFileClick(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_delFile->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_delFileClick(TObject *Sender) // 删除文件
{
if(edt_delFile->Text.IsEmpty()) return;
if(DeleteFile(edt_delFile->Text))
MessageBox(Handle,"删除文件操作成功!","提示信息",MB_OK);
else{ //删除只读文件
_rtl_chmod(edt_delFile->Text.c_str(),1,FA_ARCH);
if(DeleteFile(edt_delFile->Text))
MessageBox(Handle,"删除文件操作成功!","提示信息",MB_OK);
else
MessageBox(Handle,"删除文件操作失败!","提示信息",MB_OK);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_delDirClick(TObject *Sender) // 删除文件夹
{
// deltree(edt_Dir->Text);
String strFolder = edt_Dir->Text;
if(strFolder.IsEmpty()) return;
if(!DirectoryExists(strFolder)){
MessageBox(Handle,"删除文件夹不存在","提示信息",MB_OK);
return;
}
String strOpt = "rd ";
strOpt += strFolder + " ";
int iResult = system(strOpt.c_str());
switch(iResult){
case ENOMEM:
MessageBox(Handle,"系统内存或资源不足!","提示信息",MB_OK);
break;
case ENOENT:
MessageBox(Handle,"路径或文件错误!","提示信息",MB_OK);
break;
case ENOEXEC:
MessageBox(Handle,"执行文件格式错误!","提示信息",MB_OK);
break;
default:
break;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_FindClick(TObject *Sender) // 搜索含隐藏文件
{
lst_ListFile->Clear();
String strFolder = edt_Folder->Text;
String strFile = edt_File->Text;
if(strFolder.IsEmpty() || strFile.IsEmpty()) return;
TSearchRec strRecord;
int iAttributes = 0;
iAttributes |= faHidden;
if(FindFirst(strFolder+"//"+strFile,iAttributes,strRecord) == 0){
if(_rtl_chmod(strRecord.Name.c_str(),0) == FA_HIDDEN) //FA_systm FA_rdonly FA_hidden FA_label FA_direc FA_arch
{
_rtl_chmod(strRecord.Name.c_str(),1,FA_ARCH);
lst_ListFile->Items->Add(strFolder+"//"+strRecord.Name);
}
while(FindNext(strRecord) == 0){
_rtl_chmod(strRecord.Name.c_str(),1,FA_ARCH);
lst_ListFile->Items->Add(strFolder+"//"+strRecord.Name);
}
FindClose(strRecord);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_1Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_FileName->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_FindStrClick(TObject *Sender) // 搜索文本文件字符串
{
TMemo *mmo_1 = new TMemo(this);
mmo_1->Parent = this;
try{
mmo_1->Lines->LoadFromFile(edt_FileName->Text);
}catch(...){
MessageBox(Handle,"文本文件不能正确打开","信息提示",MB_OK);
return;
}
int iCount = 0, i = 0;
bool bFlag = false;
char p1[255],p2[255],*p3,*p4;
for(i=0; i<mmo_1->Lines->Count; i++){
StrPCopy(p1,mmo_1->Lines->Strings[i]);
p3 = p1;
while((*p3)!= '/0'){
StrPCopy(p2,edt_String->Text);
p4 = p2;
if(strlen(p4) > strlen(p3)) break;
bFlag = true;
while((*p4)!='/0' && (*p3)!='/0'){
if( *p4 != *p3){
bFlag = false;
p3++;
break;
}
p4++;
p3++;
}
if(bFlag == true) iCount++;
}
}
if(iCount){
AnsiString strResult = "找到" + IntToStr(iCount) + "个相同的字符串";
edt_Result->Text = strResult;
}
else
edt_Result->Text = "没有找到相同的字符串";
delete mmo_1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_2Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_Long->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ShortPathClick(TObject *Sender) // 长文件名转化短文件名
{
if(edt_Long->Text.IsEmpty()) return;
char shortFileName[MAX_PATH];
GetShortPathName(edt_Long->Text.c_str(),shortFileName,MAX_PATH);
edt_Short->Text = shortFileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_MultiSelectClick(TObject *Sender) // 在文件中选择多个文件
{
lst_Multi->Clear();
dlgOpen_1->Options.Clear();
dlgOpen_1->Options<<ofAllowMultiSelect<<ofFileMustExist;
if(dlgOpen_1->Execute()){
for(int i=0; i< dlgOpen_1->Files->Count; i++)
lst_Multi->Items->Add(dlgOpen_1->Files->Strings[i]);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SystemClick(TObject *Sender) // 获得系统所有文件类型
{
lst_Multi->Clear();
HKEY hKey;
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT,"",0,KEY_ENUMERATE_SUB_KEYS,&hKey)){
DWORD dwIndex = 0;
char Name[100];
SHFILEINFO shSmall;
while(RegEnumKey(hKey,dwIndex++,Name,100) == ERROR_SUCCESS){
if(Name[0] != '.') continue;
ZeroMemory(&shSmall,sizeof(shSmall));
SHGetFileInfo(Name,FILE_ATTRIBUTE_NORMAL,&shSmall,sizeof(shSmall),SHGFI_USEFILEATTRIBUTES | SHGFI_DISPLAYNAME | SHGFI_TYPENAME | SHGFI_ICON | SHGFI_SMALLICON);
AnsiString strFileType = "文件扩展名: ";
strFileType += Name;
strFileType += "/t文件类型: ";
strFileType += shSmall.szTypeName;
lst_Multi->Items->Add(strFileType);
}
::RegCloseKey(hKey);
}
MessageBox(Handle,"获取系统所有文件类型操作成功","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_BigClick(TObject *Sender) // 快速创建特大型文件
{
HANDLE hFile = CreateFile(edt_Path->Text.c_str(),GENERIC_WRITE,0,NULL,CREATE_NEW,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile == INVALID_HANDLE_VALUE){ // 错误处理
LPVOID strInfo;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&strInfo,0,NULL);
MessageBox(0,(LPCTSTR)strInfo,"信息提示",MB_OK|MB_ICONINFORMATION);
::LocalFree(strInfo);
return;
}
// 特大型文件大小
DWORD dwPtr = SetFilePointer(hFile,1024*1024*StrToInt(edt_Size->Text),NULL,FILE_BEGIN);
if(dwPtr == 0xFFFFFFFF){ //处理出错信息
LPVOID strInfo;
::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,0,GetLastError(),
MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&strInfo,0,NULL);
MessageBox(0,(LPCTSTR)strInfo,"信息提示",MB_OK|MB_ICONINFORMATION);
::LocalFree(strInfo);
return;
}
SetEndOfFile(hFile);
::CloseHandle(hFile);
MessageBox(Handle,"特大型文件创建成功","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_CreateTempClick(TObject *Sender) // 产生临时文件
{
if(edt_Bf->Text.IsEmpty()) return;
char TempPath[MAX_PATH];
GetTempPath(sizeof(TempPath),TempPath);
char TempFile[MAX_PATH];
char PreFix[3];
strcpy(PreFix,edt_Bf->Text.c_str());
GetTempFileName(TempPath,PreFix,0,TempFile);
edt_TempName->Text=TempFile;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_5Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_File_1->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_RenameClick(TObject *Sender) // 更改文件名
{
if(edt_File_1->Text.IsEmpty() || edt_File_2->Text.IsEmpty()) return;
if(RenameFile(edt_File_1->Text,edt_File_2->Text))
MessageBox(NULL,"文件更名成功","信息提示",MB_OK);
else
MessageBox(NULL,"文件更名错误","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_3Click(TObject *Sender)
{
if(dlgOpen_1->Execute())
edt_FileExt->Text = dlgOpen_1->FileName;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_ChangeExtClick(TObject *Sender) // 更改文件扩展名
{
if(edt_FileExt->Text.IsEmpty()) return;
String strFileName = ChangeFileExt(edt_FileExt->Text,edt_ChangeExt->Text);
if(RenameFile(edt_FileExt->Text,strFileName))
MessageBox(Handle,"文件扩展名修改操作成功","信息提示",MB_OK);
else
MessageBox(Handle,"文件扩展名修改操作失败","信息提示",MB_OK);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_GetAttributesClick(TObject *Sender) // 获取文件属性
{
chk_Read->Checked = false;
chk_System->Checked = false;
chk_Hidden->Checked = false;
chk_Archive->Checked = false;
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
DWORD dwAttrs = GetFileAttributes(strFileName.c_str());
if(dwAttrs & FILE_ATTRIBUTE_READONLY)
chk_Read->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_SYSTEM)
chk_System->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_HIDDEN)
chk_Hidden->Checked = true;
if(dwAttrs & FILE_ATTRIBUTE_ARCHIVE)
chk_Archive->Checked = true;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_4Click(TObject *Sender)
{
if(dlgOpen_1->Execute()){
String fileName = dlgOpen_1->FileName;
edt->Text = fileName;
btn_GetAttributesClick(Sender); // 获取文件属性
btn_GetTimeClick(Sender); // 获取文件时间属性
edt_FilePath->Text = ExpandFileName(fileName);
edt_Ext->Text = ExtractFileExt(fileName);
edt_Name->Text = ExtractFileName(fileName);
edt_PathName->Text = ExtractFilePath(fileName);
edt_View->Text = ExtractFileDir(fileName);
edt_Drive->Text = ExtractFileDrive(fileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SetAttributesClick(TObject *Sender) // 设置文件属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
DWORD dwAttrs = GetFileAttributes(strFileName.c_str());
SetFileAttributes(strFileName.c_str(),0);
if(chk_Read->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_READONLY);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_System->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_SYSTEM);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_Hidden->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_HIDDEN);
dwAttrs = GetFileAttributes(strFileName.c_str());
if(chk_Archive->Checked)
SetFileAttributes(strFileName.c_str(),dwAttrs|FILE_ATTRIBUTE_ARCHIVE);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_GetTimeClick(TObject *Sender) // 获取文件时间属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
int FileHandle = FileOpen(strFileName,fmOpenRead);
if(FileHandle == -1){
MessageBox(Handle,"文件打开不正确","信息提示",MB_OK);
return;
}
TFileTime tCreate,tAccess,tWrite;
if(GetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite)){
TSystemTime SystemTime;
FileTimeToSystemTime(&tCreate,&SystemTime);
edt_CreateTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
FileTimeToSystemTime(&tAccess,&SystemTime);
edt_AccessTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
FileTimeToSystemTime(&tWrite,&SystemTime);
edt_WriteTime->Text = IntToStr(SystemTime.wYear)+"-"+IntToStr(SystemTime.wMonth)+"-"+IntToStr(SystemTime.wDay);
}
FileClose(FileHandle);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::btn_SetTimeClick(TObject *Sender) // 设置文件时间属性
{
String strFileName = edt->Text;
if(strFileName.IsEmpty()) return;
int FileHandle = FileOpen(strFileName,fmOpenRead);
if(FileHandle == -1){
MessageBox(Handle,"文件打开不正确","信息提示",MB_OK);
return;
}
TFileTime tCreate,tAccess,tWrite;
if(!GetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite))
MessageBox(Handle,"获取文件时间属性操作失败","信息提示",MB_OK);
TSystemTime SystemTime;
String Temp = edt_CreateTime->Text;
SystemTime.wYear=Temp.SubString(1,4).ToInt();
SystemTime.wMonth=Temp.SubString(6,2).ToInt();
SystemTime.wDay= Temp.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tCreate);
String Temp2 = edt_AccessTime->Text;
SystemTime.wYear=Temp2.SubString(1,4).ToInt();
SystemTime.wMonth=Temp2.SubString(6,2).ToInt();
SystemTime.wDay=Temp2.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tAccess);
String Temp3 = edt_WriteTime->Text;
SystemTime.wYear=Temp3.SubString(1,4).ToInt();
SystemTime.wMonth=Temp3.SubString(6,2).ToInt();
SystemTime.wDay=Temp3.SubString(9,2).ToInt();
SystemTimeToFileTime(&SystemTime,&tWrite);
if(SetFileTime((void*)FileHandle,&tCreate,&tAccess,&tWrite))
MessageBox(Handle,"设置文件时间属性操作成功","信息提示",MB_OK);
else
MessageBox(Handle,"设置文件时间属性操作错误","信息提示",MB_OK);
FileClose(FileHandle);
}
相关文章推荐
- CI框架中redis缓存相关操作文件示例代码
- CI框架中redis缓存相关操作文件示例代码
- python读写文件操作示例程序
- Visual Studio 2017中使用正则修改部分内容 如何使用ILAsm与ILDasm修改.Net exe(dll)文件 C#学习-图解教程(1):格式化数字字符串 小程序开发之图片转Base64(C#、.Net) jquery遍历table为每一个单元格取值及赋值 。net加密解密相关方法 .net关于坐标之间一些简单操作
- 文件/字符串操作相关的python程序
- .NET相关操作其他文件的小程序(系列文章)
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
- 重新想象 Windows 8 Store Apps (70) - 其它: 文件压缩和解压缩, 与 Windows 商店相关的操作, app 与 web, 几个 Core 的应用, 页面的生命周期和程序的生命周期
- python读写文件操作示例程序
- 实验 5.2.8-3 对文件的任意部分加锁 1. 用 fcntl()对文件进行锁操作。 2. 完善课件中的示例程序,给出程序运行结果及分析。 给b.txt中的前10个字节加写锁
- 使用Zip Utils 解压文件的操作示例
- SQL Server 安装问题:以前的某个程序安装已在安装计算机上创建挂起的文件操作
- shell常用操作命令系列:文件相关操作命令
- 设计一程序(Copy.java),可以实现文件的复制操作
- 编写文件搜索小程序:1. 输入绝对路径以及搜索关键字,2. 搜索指定路径下(包括子文件夹)中名称包含关键字的所有文件并打印出,3. 将当前操作记录日志
- ios中文件的相关操作:读,写,删除
- 合并两个文件内容等相关操作
- 在GHOST的WINXPSP2安装SQL SERVER2000个人版时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装之前,必须重新启动计算机。”错误的解决方法
- matlab文件读取相关操作
- 以前的某个程序安装已在安装计算机上创建挂起文件操作(转载)