Learn about. NET file library

Source: Internet
Author: User

Today, I want to ask a few programmers who want to ask a file-type operation question and I cannot answer the result. I feel like I am talking nonsense, so I have to solve it myself and I have not explained it on the Internet, so I read the help of CSDN, and the idea is solved. In the past few days, I have no time to study the program. My teachers have arranged for me to listen to Marx's philosophy. I have to chat with my wife in my spare time. Today is the first day of file operations, so I have to take notes, feeling. net I/O namespace file operation type is currently the most abundant and concise in several languages I have encountered, so it is very good to learn, first draw a file class framework.


Any operating file in a programming language must be implemented by calling the application interface of the operating system ,. the. NET Framework provides powerful and unified application interfaces. the I/O namespace defines various file management, file and data stream read/write types.
I will not talk much about nonsense. Let's take a look at the examples I wrote by referring to the official Microsoft instance.
Using System;
Using System. IO;

Namespace File Operations
{
Class Test
{
Static void Main (string [] args)
{

String file = @ "D: \ test1.txt ";
If (! File. Exists (file ))
{
File. Create (file );
}

Else
{
Try
{
If (File. GetAttributes (file) & FileAttributes. Hidden) = FileAttributes. Hidden)
// If the object is hidden
{
File. SetAttributes (file, FileAttributes. Archive );
Console. WriteLine ("files are no longer hidden ");
}
Else // if the file is not hidden
{
File. SetAttributes (file, File. GetAttributes (file) | FileAttributes. Hidden );
Console. WriteLine ("file hidden ");
}
}
Catch (Exception ex)
{
Console. WriteLine (ex. Message );
}
}
Console. ReadKey ();

}
}

}

The code is simple and easy to read. That's what it means. It mainly solves the Value Method of the following code line:

If (File. GetAttributes (file) & FileAttributes. Hidden) = FileAttributes. Hidden)
How can we determine whether a file attribute contains the Hidden attribute value? Here we will follow up with MSDN to explain how to leave a comment for me if I have a high opinion, and I will also learn about it.
1. The File. GetAttributes method gets the FileAttributes of the File in this path.
2. Continue to the description of FileAttributes: This enumeration has a FlagsAttribute attribute, allowing its member values to be combined by bit.
3. Let's look at the FlagsAttribute attributes again:
FlagsAttribute is used for enumeration only when bitwise operations (AND, OR, XOR) are performed on values.
• Use the power of 2 (1, 2, 4, and 8) to define enumeration constants. This means that each flag in the enumerated constant of the combination does not overlap.
Consider creating an enumerated constant for a combination of common signs. For example, if the enumerated values used for file I/O operations include the enumerated constants Read = 1 and Write = 2, consider creating the enumerated constants ReadWrite = Read OR Write, this constant combines the Read and Write flags. In addition, in some cases, bitwise OR operations used to combine a flag may be considered as an advanced concept, which is not required in simple tasks.
Now, let's sum up. The File. GetAttributes (file) Here returns an enumeration object. FileAttributes. Hidden is only one of the attributes in the enumeration. Here is a simple example:
1 using System;
2
3 class FlagsAttributeDemo
4 {
5 enum Color1: short
6 {
7 Black = 0,
8 Red = 1,
9 Green = 2,
10 Blue = 4
11 };
12
13 [FlagsAttribute]
14 enum Color2: short
15 {
16 Black = 0,
17 Red = 1,
18 Green = 2,
19 Blue = 4
20 };
21
22 static void Main ()
23 {
24 Console. WriteLine ("test the flagsattrieline attribute is not used ");
25 Color1 MyColor1 = Color1.Red | Color1.Blue & Color1.Green;
26 // I will not run the computation first to see which one is: 0001 | 0100 & 0010 = 0001 should be Red
27 Console. WriteLine ("MyColor1 = {0}", MyColor1 );
28 Color1 MyColor_1 = Color1.Red | Color1.Blue;
29 // I will not run the computation first to see which one is: 0001 | 0100 = 0101 should be 5
30 Console. WriteLine ("MyColor_1 = {0}", MyColor_1 );
31 Console. WriteLine ("test the flagsattrieline attribute ");
32 Color2 MyColor2 = Color2.Red | Color2.Blue;
33 // I will not run the computation first to see which one is: 0001 | 0100 = 0101 should be Red, Blue
34 Console. WriteLine ("MyColor2 = {0}", MyColor2 );
35 Console. ReadKey ();
36}

The running result is as follows:



 

The above Code shows that binary operations

If (File. GetAttributes (file) & FileAttributes. Hidden) = FileAttributes. Hidden)

Is it easy? File. GetAttributes (file) & FileAttributes. Hidden should return FileAttributes. Hidden; therefore, you can determine whether the Hidden attribute is included.

Of course, I really don't understand it. It is more convenient to understand the concept of a set (| corresponds to the Union set, & corresponds to the intersection). Simply put, this is (a | B) & B = B.

OVER

From Xie xiaoge
 

Related Article

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.