C + + File property settings (hidden, read-only, encrypted, etc.)

Source: Internet
Author: User

In msdn, there are 15 properties for a file, and the properties of the file vary depending on the partition format of the Disk.
    1. GetFileAttributes Get the return value of a file property function

return field

return value

Property type

File_attribute_readonly

1

Read-only

File_attribute_hidden

2

Hide

File_attribute_system

4

System

File_attribute_directory

16

Directory

File_attribute_archive

32

Archive

File_attribute_device

64

Keep

File_attribute_normal

128

Normal

File_attribute_temporary

256

Temporary

File_attribute_sparse_file

512

Sparse files

File_attribute_reparse_point

1024

Hyperlinks or shortcuts

File_attribute_compressed

2048

Compression

File_attribute_offline

4096

Offline

File_attribute_not_content_indexed

8192

Index

file_attribute_encrypted

16384

Encryption

File_attribute_virtual

65536

Virtual

The properties of the orange tag are the public properties of the files in the Windows system, where read-only, hidden, system, archive are the four basic properties of the File. Compressed,content_indexed,encrypted only exists in NTFS Partitions.

After the file has stripped all of its properties (four basic properties), It is automatically marked as Normal. Files with both system and hidden properties are completely invisible in the system, which is a common trick for Viruses.

Commpressed and encrypted cannot coexist. Files have the Content_indexed property by default

2.setfileattributes Set file Property function

Set file Properties: setfileattributes (file name, Property Value)

SetFileAttributes (filename, file_attribute_readonly); Set to Read-only

SetFileAttributes (filename, file_attribute_hidden); Set to Hidden

SetFileAttributes (filename, file_attribute_system); Set As System

SetFileAttributes (filename, file_attribute_archive); Set to save

SetFileAttributes (filename, file_attribute_normal); Set to normal (cancel top four properties)

Set more than two properties:

    • Set to Read-only + hide

SetFileAttributes (filename, file_attribute_readonly | file_attribute_hidden);

    • Set as Read-only + Hide + system + Save

SetFileAttributes (filename, file_attribute_readonly | File_attribute_hidden _
| File_attribute_system | file_attribute_archive);

    • Cancel all settings

SetFileAttributes (filename, file_attribute_normal);

-successfully returned 1-65536, error returned 0;

-#include <windows.h> Header files;

Vc

To move and copy files, delete files

Methods for setting file properties

CopyFile

And

MoveFile

SetFileAttributes

Rename

BOOL CopyFile (

Lpctstr

Lpexistingfilename

,

Name of an existing file

Lpctstr

Lpnewfilename

,

Name of the new file

BOOL

Bfailifexists

Operation if file exists

);

Attention:

1

, the path name of the file must be the

\\

Instead of

\

\

It's not working, I've tried it.

2

Bfailifexists

The value is

TRUE

If the file you want to copy is already saved in the destination folder

on, then replication Fails. Value is

FALSE,

Overrides are Allowed.

BOOL MoveFile (

Lpctstr

Lpexistingfilename

,

File name

Lpctstr

Lpnewfilename

New file name

);

Attention:

Its copied file name and

CopyFile

is the Same.

DeleteFile

The

DeleteFile

function deletes an existing File.

BOOL DeleteFile (

Lpctstr

lpFileName

File name

);

BOOL SetFileAttributes (

Lpctstr

lpFileName

,

File name

DWORD

dwFileAttributes

Attributes

);

Attention:

dwFileAttributes

For property settings, There are the following

Int

Rename

Const

Char

*

Oldname

,

Const

Char

*

NewName

);

Oldname

And

NewName

The same is the file name (including the path),

The return value of the above three functions is

0

, the operation fails and the operation succeeds for a value other than 0.

The above two functions are contained in a

Windows.h

The header File.

Example

#include <windows.h>

#include <iostream>

Using namespace std;

void Main ()

{

CopyFile ("d:\\dsc00008. JPG "," e:\\mv\\1.jpg ", false);

MoveFile ("d:\\javahe

And

Mysql

Instance

. doc "," E:\\mv\\2.doc ");

SetFileAttributes ("e:\\mv\\2.doc", file_attribute_hidden);

Rename ("e:\\mv\\2.doc", "e:\\mv\\javahe

And

Mysql

Instance

. doc ");

cout<< "

Replication succeeded

";

}

And also

CreateFile

In

Msdn

Can be found in The. Check it out when you need it.

C + + File property settings (hidden, read-only, encrypted, etc.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.