windows api之檔案的建立

2021-10-23 03:10:15 字數 1739 閱讀 4353

在windows api中可以使用createfile來建立資料夾,其中createfilew是unicode編碼下的巨集函式,createfilew是長位元組下的巨集函式,以下程式設計環境使用unicode編碼,所以使用的是createfilew。

我們先來看一下巨集函式的原型:

winbaseapi

__out

handle

winapi

createfilew(

__in lpcwstr lpfilename,

__in dword dwdesiredaccess,

__in dword dwsharemode,

__in_opt lpsecurity_attributes lpsecurityattributes,

__in dword dwcreationdisposition,

__in dword dwflagsandattributes,

__in_opt handle htemplatefile

);

constant

generic meaning

generic_all

獲得所有許可權

generic_execute

可執行generic_read

可訪問generic_write

可寫入

constant

generic meaning

file_share_read

訪問許可權

file_share_write

寫入許可權

0不允許其他程序在請求刪除、讀取或寫入訪問許可權時開啟檔案或裝置

file_share_delete

開啟檔案的裝置被允許執行檔案的刪除

security_attributes結構體如下所示:

typedef struct _security_attributes security_attributes;

constant

generic meaning

create_always

總是建立乙個檔案,並且覆蓋以前原有的檔案

create_new

只有在檔案不存在時建立

open_always

總是開啟乙個檔案

open_existing

開啟乙個已存在的檔案

truncate_existing

開啟乙個已有檔案並且刪除檔案裡面的內容

attribute

meaning

file_attribute_normal

無屬性file_flag_no_buffering

檔案或者裝置開啟的時候不使用快取

file_flag_open_reparse_point

當開啟乙個檔案時,會返回乙個檔案控制代碼,不管控制重新解析點的過濾器是否工作

tip:重解析可以改變檔案系統過濾器對檔案的各種操作。

接著,我們來看一下code,以開啟檔案為例:

int winapi winmain(hinstance hinstance, hinstance hprevinstance,lpstr lpcmdline, int ncmdshow)

else

return 0;

}

需要注意的是,在呼叫完檔案之後,一定要使用closehandle()來關閉你開啟的檔案控制代碼,釋放掉資源。

Windows API實現檔案的查詢

在實際應用,經常要用到檔案的查詢,在前面,我們給大家介紹了如何用cfilefind類實現檔案的遍歷以及如何用列表控制項顯示。這裡,我們主要介紹一下如何使用windows api來實現檔案的查詢。1 findfirstfile 它有兩個引數,第乙個是要查詢的檔名,第二個是儲存查詢到的資訊,型別為win...

利用Windows API判斷檔案共享鎖定狀態

一 概述 鎖是作業系統為實現資料共享而提供的一種安全機制,它使得不同的應用程式,不同的計算機之間可以安全有效地共享和交換資料。要保證安全有效地操作共享資料,必須在相應的操作前判斷鎖的型別,然後才能確定資料是否可讀或可寫,從而為開發出健壯的程式提供切實依據。同樣,在windows中,檔案可以共享模式開...

用WindowsAPI實現檔案複製功能

注釋也在裡面 檔名為 copyfile.c 執行出來的exe為 copyfile.exe include include define buf size 256 int main int argc,lptstr argv 如果通過上面判斷語句的話說明引數輸入符合形式 則進行下乙個判斷 1 判斷檔案1...