磁盘空间还剩吗? [英] disk space remaining?
问题描述
.net中没有办法为给定的驱动器留下磁盘空间吗?我不能相信它,但似乎是这样。如果有人知道如何在.net库中做到这一点,我会非常感激。
谢谢,LT。
New World Order Pigs &的LT,LT ******* @ md-it.com>在消息新闻中写道:%2 **************** @ TK2MSFTNGP09.phx.gbl ....net中没有办法获取磁盘给定驱动器的剩余空间???我不能相信它,但似乎是这样。如果有人知道如何在.net库中做到这一点,我会非常感激。
谢谢,LT。
>
您可以使用管理命名空间类(和WMI)。
这里有一个小样本:
使用系统;
使用System.Management;
class Tester {
public static void Main(){
Int64 i = GetFreeSpace(" C:");
Console.WriteLine(i);
}
public static Int64 GetFreeSpace(string logicalDrive)
{
Int64 nRet = 0;
//创建查询
String strSQL =" SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID =''" + logicalDrive +"''" ;
尝试
{
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(查询);
foreach(searcher.Get()中的ManagementBaseObject驱动器)
{
UInt64 u =(UInt64)驱动器["自由空间"]; //获取自由空间属性
nRet =(Int64)u;
}
}
catch(例外e)
{
Console.WriteLine(e.ToString());
}
返回nRet / 1024; //返回KB
} // GetFreeSpace
}
Willy。
谢谢威利。我尝试了类似的东西,它似乎没有工作。
我希望.net库中有一些东西允许直接的
函数调用和不是System.Management类。对于这一点我仍然处于震惊状态,并且不能相信你不能做像
System.IO这样的事情。 DirectoryInfo.GetFreeSpace"或类似的东西...
谢谢威利。显然我必须继续使用系统管理
课程。
Willy Denoyette [MVP]" <无线************* @ pandora.be>在消息中写道
新闻:#r ************* @ TK2MSFTNGP09.phx.gbl ...
" ;新世界秩序猪 &的LT,LT ******* @ md-it.com>在留言中写道
news:%2 **************** @ TK2MSFTNGP09.phx.gbl ...在.net中没有办法为给定的驱动器剩余磁盘空间吗?
我不敢相信,但似乎是这样。如果有人知道在.net库中使用
的方式,我会非常感激。
谢谢,LT。
>
您可以使用Management命名空间类(和WMI)。
这里有一个小样本:
使用System;
使用System.Management;
class Tester {
public static void Main(){
Int64 i = GetFreeSpace(" C:");
Console.WriteLine(i);
} <公共静态Int64 GetFreeSpace(string logicalDrive)
{
Int64 nRet = 0;
//创建查询
String strSQL =" SELECT FreeSpace FROM Win32_LogicalDisk WHERE
DeviceID =''" + logicalDrive +"''" ;尝试
{SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new
ManagementObjectSearcher(query);
foreach(Searcher.Get()中的ManagementBaseObject驱动器)
{
UInt64 u =(UInt64)drive [" FreeSpace"]; //获取freespace属性
nRet =(Int64)u;
}
}
catch(例外e)
{
控制台。 WriteLine(e.ToString());
}
返回nRet / 1024; //返回KB
} // GetFreeSpace
}
Willy。
>
DirectoryInfo没有GetFreeSpace方法,因为它设计为
常规目录(
将FreeSpace引用到常规目录是没有意义的。
System.Management类提供了一种检索信息的方式
来自WMI,
WMI的功能是纪念碑和强大的,所以我认为没有必要使用
Net来创建一个新类或
方法。(如果.Net真的创建了一个新类,它将与
WMI引擎盖互通)
如果你仍然这样做不想使用System.Management类,你可以将
引用到FileSystemObject,如下所示:
添加Microsoft Scripting RunTIme 1.0来自添加参考命令,
" COM"标签页,
Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives = System.IO.Directory.GetLogicalDrives();
for(int i = 0; i< logDrives.GetUpperBound(0)+1; i ++)
{
string disk = logDrives [ i] .Substring(0,2);
string param =" Win32_LogicalDisk =''" + disk +"''" ;;
Scripting.Drive thisdrive = FSO.GetDrive(logDrives [i]);
if(thisdrive.DriveType == Scripting.DriveTypeConst.Fix ed)
{
MessageBox.Show(thisdrive.Path +" FreeSpace:" + thisdrive.FreeSpace);
}
}
希望这会有所帮助,
祝你好运,
Jeffrey Tan
微软在线合作伙伴支持
安全! - www.microsoft.com/security
此帖子按原样提供没有保证,也没有赋予任何权利。
--------------------
|来自:新世界秩序猪 < lt ******* @ md-it.com>
|参考文献:< #B ************** @ TK2MSFTNGP09.phx.gbl>
< #r *********** **@TK2MSFTNGP09.phx.gbl>
|主题:Re:剩余磁盘空间?
|日期:2003年9月22日星期一17:02:35 -0600
|行数:69
| X-Priority:3
| X-MSMail-Priority:正常
| X-Newsreader:Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE:由Microsoft MimeOLE制作V6.00.2800.1106
|消息ID:< #j ************* @ TK2MSFTNGP11.phx.gbl>
|新闻组:microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host:64.207.45.130
|路径:cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
|外翻:cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG:microsoft.public.dotnet.languages.csharp
|
|谢谢威利。我尝试了类似的东西,但似乎没有
工作。
|我希望.net库中有一些东西允许
直接
|函数调用而不是System.Management类。我还在州里
|关于这一点感到震惊并且不能相信你不能做某事
喜欢
| " System.IO.DirectoryInfo.GetFreeSpace"或类似的东西...
|
|谢谢威利。显然我必须继续玩系统
管理
|虽然上课。
|
|
| Willy Denoyette [MVP]" <无线************* @ pandora.be>在消息中写道
|新闻:#r ************* @ TK2MSFTNGP09.phx.gbl ...
| >
| > 新世界秩序猪 &的LT,LT ******* @ md-it.com>在消息中写道
|新闻:%2 **************** @ TK2MSFTNGP09.phx.gbl ...
| > >在.net中没有办法为给定的
驱动器剩余磁盘空间???
|我
| > >不能相信它,但似乎是这样。如果有人知道某种方式
|要做什么
| > >在.net库中,我会非常感激。
| > >
| > >谢谢,LT。
| > >
| > >
| >
| >您可以使用Management命名空间类(和WMI)。
| >这是一个小样本:
| >
| >使用系统;
| >使用System.Management;
| > class Tester {
| > public static void Main(){
| > Int64 i = GetFreeSpace(" C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive)
| > {
| > Int64 nRet = 0;
| > //创建查询
| > String strSQL =" SELECT FreeSpace FROM Win32_LogicalDisk WHERE
|的DeviceID =" + logicalDrive +"''" ;
| >试试
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher搜索者=新
| ManagementObjectSearcher(查询);
| >
| > foreach(在searcher.Get()中的ManagementBaseObject驱动器
| > {
| > UInt64 u =(UInt64)drive [" FreeSpace"]; //获取自由空间属性
| > nRet =(Int64)u;
| > }
| >
| > }
| > catch(例外e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| >返回nRet / 1024; //返回KB
| > } // GetFreeSpace
| >
| > }
| >
| >威利。
| >
| >
|
|
|
Is there no way in .net to get disk space remaining for a given drive??? I
can''t believe it and yet is seems to be so. If someone knows of a way to do
this in the .net libraries I''d be very much appreciative.
Thanks, LT.
"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...Is there no way in .net to get disk space remaining for a given drive??? I
can''t believe it and yet is seems to be so. If someone knows of a way to do
this in the .net libraries I''d be very much appreciative.
Thanks, LT.
You could use the Management namespace classes (and WMI).
Here''s a small sample:
using System;
using System.Management;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C:");
Console.WriteLine(i);
}
public static Int64 GetFreeSpace(string logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE DeviceID=''" + logicalDrive + "''" ;
try
{
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementBaseObject drive in searcher.Get())
{
UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
nRet = (Int64)u;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return nRet/1024; // return KB
} // GetFreeSpace
}
Willy.
Thanks Willy. I tried something similar to this and it didn''t seem to work.
I was hoping there was something in the .net libraries that allowed a direct
function call and not the System.Management class. I''m still in a state of
shock regarding this and just can''t believe you can''t just do something like
"System.IO.DirectoryInfo.GetFreeSpace" or something similar...
Thanks Willy. Apparently I have to keep playing with the System Management
class though.
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:#r*************@TK2MSFTNGP09.phx.gbl...
"New World Order Pigs" <lt*******@md-it.com> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...Is there no way in .net to get disk space remaining for a given drive??? I can''t believe it and yet is seems to be so. If someone knows of a way to do this in the .net libraries I''d be very much appreciative.
Thanks, LT.
You could use the Management namespace classes (and WMI).
Here''s a small sample:
using System;
using System.Management;
class Tester {
public static void Main() {
Int64 i = GetFreeSpace("C:");
Console.WriteLine(i);
}
public static Int64 GetFreeSpace(string logicalDrive )
{
Int64 nRet = 0;
// Create a query
String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
DeviceID=''" + logicalDrive + "''" ; try
{
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach(ManagementBaseObject drive in searcher.Get())
{
UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
nRet = (Int64)u;
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
return nRet/1024; // return KB
} // GetFreeSpace
}
Willy.
Hi,
DirectoryInfo does not have GetFreeSpace method, because it is design for
regular directory(
It makes no sense referring FreeSpace for regular directory).
The System.Management class provided a way of retreiving the information
from WMI, the
WMI''s function is monument and strong, so I think there is no need for the
Net to create a new class or
method for this.(If .Net really create a new class, it will interop with
WMI under hood)
If you still do not want to use the System.Management class, you can refer
to the FileSystemObject, like this:
Add the "Microsoft Scripting RunTIme 1.0" from the "Add Reference" command,
"COM" tab page,
Scripting.FileSystemObject FSO = new Scripting.FileSystemObjectClass();
string [] logDrives=System.IO.Directory.GetLogicalDrives();
for ( int i =0;i< logDrives.GetUpperBound(0)+1;i++)
{
string disk=logDrives[i].Substring(0,2);
string param="Win32_LogicalDisk=''"+disk+"''";
Scripting.Drive thisdrive=FSO.GetDrive(logDrives[i]);
if (thisdrive.DriveType==Scripting.DriveTypeConst.Fix ed )
{
MessageBox.Show(thisdrive.Path+" FreeSpace:"+thisdrive.FreeSpace );
}
}
Hope this helps,
Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
| From: "New World Order Pigs" <lt*******@md-it.com>
| References: <#B**************@TK2MSFTNGP09.phx.gbl>
<#r*************@TK2MSFTNGP09.phx.gbl>
| Subject: Re: disk space remaining?
| Date: Mon, 22 Sep 2003 17:02:35 -0600
| Lines: 69
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
| Message-ID: <#j*************@TK2MSFTNGP11.phx.gbl>
| Newsgroups: microsoft.public.dotnet.languages.csharp
| NNTP-Posting-Host: 64.207.45.130
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTN GP11.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.csharp:186661
| X-Tomcat-NG: microsoft.public.dotnet.languages.csharp
|
| Thanks Willy. I tried something similar to this and it didn''t seem to
work.
| I was hoping there was something in the .net libraries that allowed a
direct
| function call and not the System.Management class. I''m still in a state
of
| shock regarding this and just can''t believe you can''t just do something
like
| "System.IO.DirectoryInfo.GetFreeSpace" or something similar...
|
| Thanks Willy. Apparently I have to keep playing with the System
Management
| class though.
|
|
| "Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
| news:#r*************@TK2MSFTNGP09.phx.gbl...
| >
| > "New World Order Pigs" <lt*******@md-it.com> wrote in message
| news:%2****************@TK2MSFTNGP09.phx.gbl...
| > > Is there no way in .net to get disk space remaining for a given
drive???
| I
| > > can''t believe it and yet is seems to be so. If someone knows of a way
| to do
| > > this in the .net libraries I''d be very much appreciative.
| > >
| > > Thanks, LT.
| > >
| > >
| >
| > You could use the Management namespace classes (and WMI).
| > Here''s a small sample:
| >
| > using System;
| > using System.Management;
| > class Tester {
| > public static void Main() {
| > Int64 i = GetFreeSpace("C:");
| > Console.WriteLine(i);
| > }
| >
| > public static Int64 GetFreeSpace(string logicalDrive )
| > {
| > Int64 nRet = 0;
| > // Create a query
| > String strSQL = "SELECT FreeSpace FROM Win32_LogicalDisk WHERE
| DeviceID=''" + logicalDrive + "''" ;
| > try
| > {
| > SelectQuery query = new SelectQuery(strSQL);
| > ManagementObjectSearcher searcher = new
| ManagementObjectSearcher(query);
| >
| > foreach(ManagementBaseObject drive in searcher.Get())
| > {
| > UInt64 u = (UInt64)drive["FreeSpace"]; // Get freespace property
| > nRet = (Int64)u;
| > }
| >
| > }
| > catch (Exception e)
| > {
| > Console.WriteLine(e.ToString());
| > }
| > return nRet/1024; // return KB
| > } // GetFreeSpace
| >
| > }
| >
| > Willy.
| >
| >
|
|
|
这篇关于磁盘空间还剩吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!