VB.NET - really in nid of help

Asked By viper ® on 07-Nov-05 08:39 PM
my task needs me to do a simple programme to detect the hard disk capacity and free space in the pc....i am doing a windows stand alone application.. where by... i will create the .exe and place it to every remote pc.. so that when i schedule the exe to run..... the process will be run and get the data needed... 
anyway.. i just wana ask....  is there any possibility for me to get the all drive available at the pc and then get the capacity of the free space?
coz now.. with those coding... it can onli detect the drive IF ONLY i state at the coding
sample coding
Dim fs, d, n 
fs = Server.CreateObject("Scripting.FileSystemObject") 
d = fs.GetDrive("c:") 
TextBox1.Text = d.TotalSize 
d = Nothing 
fs = Nothing 
so lets say.... if the hard disk does have 3partitions, anyway i can find it with my coding that detect there are C: , D:, E:, as well as getting the totalsize or freespace of the hard disk?
thank you/....
*really in need of help....

try this for both

Asked By Venkat K on 07-Nov-05 09:00 PM
free space and getting the partition name
Public Function GetDiskSpace() As System.UInt64
Dim diskClass As _
New System.Management.ManagementClass("Win32_LogicalDisk")
Dim disks As System.Management.ManagementObjectCollection = _
diskClass.GetInstances()
Dim disk As System.Management.ManagementObject
Dim space As System.UInt64
For Each disk In disks
If CStr(disk("Name")) = "C:" Then
space = CType(disk("FreeSpace"), System.UInt64)
End If
Next disk
Return space
End Function

how?

Asked By viper ® on 07-Nov-05 10:14 PM
er.... tis is the function onli right? so.... lets say if onli when i click at button to display it out.... so i put a msgbox() there?
pass back wat variable?
hmm,. sorri coz i am still new in vbnet...
Asked By Sunitha Nambari on 08-Nov-05 01:04 AM
hi,
Check this:
step1:Creating File system object
          <%
            set alldrive = server.createobject("scripting.filesystemobject")
           %>
       The above line create a filesystem object named alldrive
step2: Getting all drives in a system
<form name="myform" action="process.asp" method="post"> 
<select name="mydrives" onchange="document.myform.submit();">
<%
for each x in alldrive.drives
%>
 <option value="<%=x%>"><%=x%></option>
<%
next
%> </select>
</form> 
     By executing above code we can get all drives in a system in a combo box,if the combo box value is chaged,the form will be submitted to process.asp.
(ii)process.asp
                          This page is used to display all drive informations.
step1: Getting Drive Informations
                   <%
disk = request.form("mydrives")
set nowdisk = server.createobject("scripting.filesystemobject")
set  v = nowdisk.getdrive(disk)
 if(v.isready) then 'Check if the drive is ready or not
          dletter = v.driveletter 'Getting drive letter
          dtype = v.drivetype 'Getting Drive Type 
          aspace = v.availablespace 'Getting Available Space
          fsystem = v.filesystem 'Getting File System
          fspace = v.freespace 'Getting Free Space
          snumber = v.serialnumber 'Getting Serial Number
          tsize = v.totalsize 'Getting Total Size
          vname = v.volumename 'Getting Volume Name 
step2: Assigning Drive Types
             if(dtype = 1)then
             dt = "Floppy"
             else if(dtype=2) then
             dt = "Hard Disk"
             else if(dtype=4) then
             dt = "CD ROM"
             end if
             end if
             end if  
step3: Calcuating Ratio
     usedspace = tsize - aspace
     dd = (usedspace / tsize) * 10000 
     used = sqr(dd)  %>
step4: Drawing used space and totalspace
    <center><span style="
width:20;height:20;
background-color:green;
clip:rect()"></span><b>Available Space</b><br>
<span style="
width:20;height:20;
background-color:red;
clip:rect()">
</span><b>Used Space</b>
</center>
<br><br><br>
<center><div style="position:absolute;
width:100;height:100;
background-color:green;
clip:rect()">
</div>
<div style="position:absolute;
width:<% response.write(used)%>;height:<% response.write(used)%>;
background-color:red;
clip:rect()">
</div>
step5 :Writing Drive Informations
<table  style="color:maroon;font-weight:bold;border:ridge 1 silver">
<tr><td>Drive Name:</td>
<td><%=(nowdisk.getdrivename(disk)) %></td></tr>
<tr><td>Drive Type:</td><td><% =dt%></td></tr>
<tr><td>Total Size:</td><td><% =tsize%> Bytes</td></tr>
<tr><td>Available Space:</td><td><%=aspace %> Bytes</td></tr>
<tr><td>Free Space:</td><td><%=fspace %> Bytes</td></tr>
<tr><td>Used Space:</td><td><% =usedspace %> Bytes</td></tr>
<tr><td>File System:</td><td><%=fsystem %></td></tr>
<tr><td>Serial Number:</td><td><%=snumber%></td></tr>
</table> 
step 6:Showing error message if the drive is not ready
      <%else%>
<script>
alert("Sorry the requested drive is not ready");
</script>
<%
 end if
%>
Regards,
Suneetha
Go thru this
Asked By Sunitha Nambari on 08-Nov-05 01:05 AM
hi,
Go thru this as well,it has info on hard disk
http://www.codeproject.com/vbscript/VvvHardwareInfo.asp?df=100&forumid;=35020&exp;=0&select;=1092060
Regards,
Suneetha
thanks but another problem in it
Asked By viper ® on 08-Nov-05 02:13 AM
hi suneetha....
thanks for the link... 
anyway.. i did it in this way and it works...
Dim shostname As String
        shostname = System.Net.Dns.GetHostName
        Label2.Text = shostname
        'Connection credentials to the remote computer - not needed if the logged in account has access 
        Try
            Dim oConn As New ConnectionOptions
            oConn.Username = "UserName"
            oConn.Password = "Password"
            Dim svlDetails As String = ""
            Dim svlDetails1 As String = ""
            Dim svlDetails2 As String = ""
            Dim oMs As New System.Management.ManagementScope("\\<MachineName>", oConn)
            'get Fixed disk stats 
            Dim oQuery As New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")
            'Execute the query 
            'For remote Machine 
            'Dim oSearcher As New ManagementObjectSearcher( oMs, oQuery) 
            'For local machine 
            Dim oSearcher As New ManagementObjectSearcher(oQuery)
            'Get the results 
            Dim oReturnCollection As ManagementObjectCollection = oSearcher.Get()
            'loop through found drives and write out info 
            For Each oReturn As ManagementObject In oReturnCollection
                ' Disk name 
                svlDetails &= "Name : " + oReturn("Name").ToString() & vbCrLf
                ' Free Space in bytes 
                svlDetails1 &= "FreeSpace: " + oReturn("FreeSpace").ToString() & vbCrLf
                ' Size in bytes 
                svlDetails2 &= "Size: " + oReturn("Size").ToString() & vbCrLf
            Next
            TextBox1.Text = svlDetails
            TextBox2.Text = svlDetails1
            TextBox3.Text = svlDetails2
        Catch ex As Exception
            'MsgBox(ex.ToString)
        End Try
so my textbox1 will s how the drive.... yupe it really work .. if i have one drive C then it will display C in my textbox1... however.. if there are more than a drive lets say 2 drive C and D...
in my textbox1 it will show..... 
C || D
tis doesnt work.. coz i plan to insert the data to my database...so hopefully iwilll reserver some text box(i) so that if there are more than one drive, the second drive will be ssaved in another textbox... can it be done?
using array?
thanks...