Browse DevX
Sign up for e-mail newsletters from DevX


Using the .NET File System Object Model : Page 2

The .NET file system object model supplies three groups of related functions—information about files and directories, ad hoc methods for manipulating paths, and tools to create and manage files of any type. The ability to manage files comes from the System.IO namespace.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Copying and Deleting Files
To make a copy of the current file, you can use the CopyTo method, which comes with two overloads. Both overloads copy the file to another file but the first overload just disallows overwriting, while the other gives you a chance to control overwriting through a Boolean parameter.

FileInfo fi = fi.CopyTo("NewFile.txt", true);

Notice that both methods require that the first argument be a filename. It can't be the name of a directory where you want the file to be copied. If you use a directory name, that will be the name of the output file.

The Delete method permanently deletes the file from disk. Using this method, there is no way to programmatically send the deleted file to the recycle bin. To put a file in the recycle bin you must resort to creating a .NET wrapper for the Win32 API function that does that. The API function you need is named SHFileOperation.

The Attributes property indicates the file system attributes of the given file. In order to set or read an attribute, the file must already exist and the application must have access to it. To write an attribute value to a file, you must also have a write permission, otherwise the FileIOPermissionAccess exception is raised. The attributes of a file are expressed using the FileAttributes type. (See Table 4.)

Table 4: The FileAttributes enumeration.




Indicates that the file is an archive.


The file is compressed.


Not currently used. Reserved for future use.


The file is a directory.


The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and directories but not necessarily that all current files are encrypted.


The file is hidden and doesn't show up in directory listings.


The file has no other attributes set. Note that this attribute is valid only if used alone.


The file should not be indexed by the system indexing service.


The file is offline and its data is not immediately available.


The file is read-only.


The file contains a reparse point, which is a block of user-defined data associated with a file or a directory. Requires an NTFS file system.


The file is a sparse file. Sparse files are typically large files whose data are mostly zeros. Requires an NTFS file system.


The file is a system file, part of the operating system or used exclusively by the operating system.


The file is temporary and can be deleted by the application any time soon.

The values in the table correspond to those defined in the Win32 SDK. Notice that not all attributes are applicable to both files and directories. You set attributes on a file using code as in the code snippet below.

// Make the file read-only and hidden FileInfo fi = new FileInfo("mydoc.txt") fi.Attributes = FileAttributes.ReadOnly | FileAttributes.Hidden;

Note you cannot set all of the attributes listed in Table 4 through the Attributes property. For example, the system assigns the Encrypted and the Compressed attributes only if the file is contained in an encrypted or compressed folder. Likewise, you can give a file a reparse point or you can mark is as a sparse file only through specific API functions and only on NTFS volumes.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date