⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileoperatedlg.cpp

📁 wince 文件操作例子,wince file operation
💻 CPP
字号:
// FileOperateDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileOperate.h"
#include "FileOperateDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CFileOperateDlg dialog

CFileOperateDlg::CFileOperateDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFileOperateDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFileOperateDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFileOperateDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFileOperateDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFileOperateDlg, CDialog)
	//{{AFX_MSG_MAP(CFileOperateDlg)
	ON_BN_CLICKED(IDC_FINDFILE, OnFindfile)
	ON_BN_CLICKED(IDC_READWRITE_FILE, OnReadwriteFile)
	ON_BN_CLICKED(IDC_FILEDIALOG, On_Filedialog)
	ON_BN_CLICKED(IDC_DELETE, OnDelete)
	ON_BN_CLICKED(IDC_COPY, OnCopy)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFileOperateDlg message handlers

BOOL CFileOperateDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}



void CFileOperateDlg::OnFindfile() 
{
	HANDLE hFile = NULL;               
	WIN32_FIND_DATA *lpwfdFile = new WIN32_FIND_DATA[10];
	BOOL bContinue = TRUE;
	int i = 0;
	hFile = FindFirstFile (L"\\ONE.TXT",&lpwfdFile[i]);	
	if (hFile == INVALID_HANDLE_VALUE)
	{
		delete[] lpwfdFile;	
		AfxMessageBox(L"Find Nothing");
		return;
	}
	else
	{
		delete[] lpwfdFile;	
		AfxMessageBox(L"Get it");
		return;
	}
	while (bContinue) 
	{
		bContinue = FindNextFile(hFile, &lpwfdFile[++i]);		
	}	
	FindClose (hFile); 
	
}

void CFileOperateDlg::OnReadwriteFile() 
{

  HANDLE hFile, hAppend;
  DWORD dwBytesRead, dwBytesWritten, dwPos;
  char buff[4096];

  // Open the existing file.

  hFile = CreateFile (TEXT("\\ONE.TXT"),      // Open One.txt.
                      GENERIC_READ,           // Open for reading
                      0,                      // Do not share
                      NULL,                   // No security
                      OPEN_EXISTING,          // Existing file only
                      FILE_ATTRIBUTE_NORMAL,  // Normal file
                      NULL);                  // No template file

  if (hFile == INVALID_HANDLE_VALUE)
  {
    // Your error-handling code goes here.
    AfxMessageBox(L"Could not open ONE.TXT");
    return;
  }

  // Open the existing file, or, if the file does not exist,
  // create a new file.

  hAppend = CreateFile (TEXT("\\TWO.TXT"),		// Open Two.txt.
                        GENERIC_WRITE,			// Open for writing
                        0,						// Do not share
                        NULL,					// No security
                        OPEN_EXISTING,			// Open or create
                        FILE_ATTRIBUTE_NORMAL,	// Normal file
                        NULL);					// No template file

  if (hAppend == INVALID_HANDLE_VALUE)
  {
    AfxMessageBox(L"Could not open TWO.TXT");
    CloseHandle (hFile);            // Close the first file.
    return;
  }

  // Append the first file to the end of the second file.

  do
  {
    if (ReadFile (hFile, buff, 4096, &dwBytesRead, NULL))
    {
      dwPos = SetFilePointer (hAppend, 0, NULL, FILE_END);
      WriteFile (hAppend, buff, dwBytesRead,
                 &dwBytesWritten, NULL);
    }
  }
  while (dwBytesRead == 4096);

  // Close both files.

  CloseHandle (hFile);
  CloseHandle (hAppend);

  return;

	
}



void CFileOperateDlg::On_Filedialog() 
{
  // Create an Open dialog; the default file name extension is ".txt"
  CFileDialog fileDlg (TRUE, _T("*.TXT"),NULL,NULL,L"TEXT Files (*.TXT)|*.TXT|All Files (*.*)|*.*||");
  
  // Display the file dialog. When the user clicks OK, fileDlg.DoModal()
  // returns IDOK.
  if( fileDlg.DoModal ()==IDOK )
  {
	CString pathName = fileDlg.GetPathName();
    // Implement opening and reading of the file here.

    // Change the title of the window to the title of the opened file.
    CString fileName = fileDlg.GetFileTitle ();
  
    SetWindowText(fileName);
  }
}



void CFileOperateDlg::OnDelete() 
{
	if(DeleteFile(L"\\ONE.TXT"))
		AfxMessageBox(L"Delete succeed");
	else
		AfxMessageBox(L"Delete faild");	
}

void CFileOperateDlg::OnCopy() 
{
	if (CopyFile (L"\\ONE.TXT", L"ONE_BAK.TXT", false)) //复制文件,如果返回为真表示成功,则
		AfxMessageBox (L"Copy succeed!"); 
	else
		AfxMessageBox (L"Copy faild!"); 
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -