To learn about the. NET file library, you have to rely on yourself.

Source: Internet
Author: User

Today, I want to ask a few questions about my career.ProgramClerk, I couldn't answer the results of a file operation question. I felt like I was talking nonsense and had to solve it myself. I didn't explain it on the Internet, so I read the help of csdn, if it comes to inspiration, it can be 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.

AnyProgramming LanguageOperationCompositionThe application interface of the operating system must be called ,. 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 file 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 ();

}
}

}

CodeIt's easy to understand. 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:

Enumeration is used only when bitwise operations (and, Or, XOR) are performed on values.FlagsattributeCustom Attributes.

    • Use the power of 2 (1, 2, 4, 8, etc.) 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 contain enumeration constantsRead = 1AndWrite = 2, Consider creating enumerated ConstantsReadwrite = read or write, This constant is combinedReadAndWriteFlag. 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 if the flagsattribute attribute is not used " );
25 Color1 mycolor1 = color1.red | color1.blue & color1.green;
26 // I will not run the computation 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. Let's see which one is: 0001 | 0100 = 0101. It should be 5.
30 Console. writeline ( " Mycolor_1 = {0} " , Mycolor_1 );
31 Console. writeline ( " Test the flagsattribute attribute. " );
32 Color2 mycolor2 = color2.red | color2.blue;
33 // I will not run the computation to see which one is: 0001 | 0100 = 0101 should be red, blue
34 Console. writeline ( " Mycolor2 = {0} " , Mycolor2 );
35 Console. readkey ();
36 }
37 }

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, my wife called ..................................... ....

 

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.