. NET System.IO.DriveInfo analysis, personal feeling m$ a bit scrap __.net

Source: Internet
Author: User

author:kj021320
blog:http://blog.csdn.net/kj021320/
Team:i.s.t.o

These days are busy writing shell, why to write Shell, a more in-depth understanding of a language. For example, IO SOCKET SYS DB can be used to write a shell to fully understand learning.
There is no class in. NET 1.1 that contains information about getting disk, which is estimated to be compatible with other platforms, hehe. Later, other platforms do not use. NET Ms. and in order to strengthen. NET feature, so in
. NET2 has a class that directly obtains disk information later. Above reasoning skillful individual yy

OK now use
Reflector Open the decompile. NET Core Class library.
and find DriveInfo.
(Feel Java this aspect is really too that.) Hey. JAD is not as convenient and intelligent as others, and very comfortable to operate.
Found it
Driveformat
Now let's see how inside. NET Gets the format of the disk
I joined the following comments
public string Driveformat
{
Get
{
Create a disk label name to allow the system to return
StringBuilder volumename = new StringBuilder (50);
Get System file format name
StringBuilder filesystemname = new StringBuilder (50);
Set Error mode
int newmode = Win32native.seterrormode (1);
Try
{
int num;
int num2;
int num3;
The core statement. Call Win32API's GetVolumeInformation
if (! Win32native.getvolumeinformation (this. Name, VolumeName, out num, out num2, out num3, Filesystemname, 50)
{
To determine if there are calls with no error message
int errorcode = Marshal.GetLastWin32Error ();
if (ErrorCode = 13)
{
ErrorCode = 15;
}
__error.winiodriveerror (this. Name, errorcode);
}
}
Finally
{
Win32native.seterrormode (Newmode);
}
Then return to the System file format
return filesystemname.tostring ();
}
}

The above should be more clear to see. And look at win32native how an inner class declares the Win32API
[DllImport ("kernel32.dll", CharSet=CharSet.Auto, Setlasterror=true)]
Internal static extern bool GetVolumeInformation (string drive, StringBuilder volumename, int volumenamebuflen, out int Vol SerialNumber, out int maxfilenamelen, out int filesystemflags, StringBuilder filesystemname, int filesystemnamebuflen);

It's easy to see.

And look at one of the following properties to get the volume label
There's a set method, and I'm not going to post it.
public string Volumelabel
{
Get
{
StringBuilder volumename = new StringBuilder (50);
StringBuilder filesystemname = new StringBuilder (50);
int newmode = Win32native.seterrormode (1);
Try
{
int num;
int num2;
int num3;
if (! Win32native.getvolumeinformation (this. Name, VolumeName, out num, out num2, out num3, Filesystemname, 50)
{
int errorcode = Marshal.GetLastWin32Error ();
if (ErrorCode = 13)
{
ErrorCode = 15;
}
__error.winiodriveerror (this. Name, errorcode);
}
}
Finally
{
Win32native.seterrormode (Newmode);
}
return volumename.tostring ();
}
}
After reading this code you will find the same as the format code to get the disk
It just doesn't come back.

Now let's see how he gets the disk capacity.

Public long TotalSize
{
Get
{
Get total disk size
Long num2;
int newmode = Win32native.seterrormode (1);
Try
{
These two are free spaces.
Long num;
Long num3;
if (! Win32native.getdiskfreespaceex (this. Name, out num, out num2, out num3)
{
__error.winiodriveerror (this. Name);
}
}
Finally
{
Win32native.seterrormode (Newmode);
}
return disk size
return num2;
}
}
The following is the definition of WIN32API
[DllImport ("kernel32.dll", CharSet=CharSet.Auto, Setlasterror=true)]
Internal static extern bool GetDiskFreeSpaceEx (string drive, out long freebytesforuser, out long totalbytes, out long free Bytes);

Let's look at the way to get free space.
Public long Totalfreespace
{
Get
{
Long num3;
int newmode = Win32native.seterrormode (1);
Try
{
Long num;
Long num2;
if (! Win32native.getdiskfreespaceex (this. Name, out num, out num2, out num3)
{
__error.winiodriveerror (this. Name);
}
}
Finally
{
Win32native.seterrormode (Newmode);
}
return num3;
}
}
Call Win32API all the same just return not the same
And keep looking at the following
Public long Availablefreespace
{
Get
{
Long num;
int newmode = Win32native.seterrormode (1);
Try
{
Long num2;
Long num3;
if (! Win32native.getdiskfreespaceex (this. Name, out num, out num2, out num3)
{
__error.winiodriveerror (this. Name);
}
}
Finally
{
Win32native.seterrormode (Newmode);
}
return num;
}
}

After reading, there is no language. I personally feel very sb every time to get a call to the need to return, do not need data thrown away.
Waste of resources AH. Why not put the corresponding data into an array, the next time the call will be directly available.
If you need to refresh to see if the disk status changes, it can also provide a refresh method.

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.