-
Jan 11th, 2018, 08:24 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Harddrive - Format Free Space
hello
i am trying to get a hard drives free and total space to display in a progressbar not sure what im doing wrong here any ideas?
Code:
Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ProgressBar1.Maximum = NthField(FormatSize(HarddriveTotalFreeSpace), ".", 1) 'show left side of dots ..
ProgressBar1.Value = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1)
'get the size
Label6.Text = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) + VBSpace + NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
End Sub
HarddriveAvailableFreeSpace and HarddriveTotalFreeSpace are the same size
Code:
Public VBSpace As String = " "
Public Function HarddriveAvailableFreeSpace() As Long
For Each curDrive As DriveInfo In My.Computer.FileSystem.Drives
If curDrive.DriveType = DriveType.Fixed Then
Dim theFreeSpace As Long = curDrive.AvailableFreeSpace
Return theFreeSpace
End If
Next
End Function
Public Function HarddriveTotalFreeSpace() As Long
For Each curDrive As DriveInfo In My.Computer.FileSystem.Drives
If curDrive.DriveType = DriveType.Fixed Then
Dim theFreeSpace As Long = curDrive.TotalFreeSpace
Return theFreeSpace
End If
Next
End Function
Public Function FormatSize(ByVal BytesCaller As ULong) As String
Dim DoubleBytes As Double
Try
Select Case BytesCaller
Case Is >= 1099511627776
DoubleBytes = CDbl(BytesCaller / 1099511627776) 'TB
Return FormatNumber(DoubleBytes, 2) & " TB"
Case 1073741824 To 1099511627775
DoubleBytes = CDbl(BytesCaller / 1073741824) 'GB
Return FormatNumber(DoubleBytes, 2) & " GB"
Case 1048576 To 1073741823
DoubleBytes = CDbl(BytesCaller / 1048576) 'MB
Return FormatNumber(DoubleBytes, 2) & " MB"
Case 1024 To 1048575
DoubleBytes = CDbl(BytesCaller / 1024) 'KB
Return FormatNumber(DoubleBytes, 2) & " KB"
Case 0 To 1023
DoubleBytes = BytesCaller ' bytes
Return FormatNumber(DoubleBytes, 2) & " bytes"
Case Else
Return ""
End Select
Catch
Return ""
End Try
End Function
Public Function NthField(ByVal expression As String, ByVal separator As String, ByVal fieldNum As Long) As String
On Error Resume Next
Dim fields() As String
fields = Split(expression, separator, , vbTextCompare)
NthField = fields(fieldNum - 1)
End Function
-
Jan 11th, 2018, 08:43 AM
#2
Re: Harddrive - Format Free Space
You have used HarddriveAvailableFreeSpace twice here:
Code:
Label6.Text = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) +
VBSpace +
NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
End Sub
I'm not entirely sure of the differences between AvailableFreeSpace and TotalFreeSpace, while I assume they are based on cluster sizes, the differences may not be that large, so the "unlit" section of the progressbar may be too small to be obvious.
edit: I also notice that the functions HarddriveAvailableFreeSpace and HarddriveTotalFreeSpace each apply to a single random hard-drive (the first one found by the loop, which will be dependent on factors outside of your code), so you may not actually be getting the information about the same drive.
Last edited by si_the_geek; Jan 11th, 2018 at 08:47 AM.
-
Jan 11th, 2018, 11:36 AM
#3
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
this label just filters out the dots splits the . and the space with the GB so it looks like the image below is my app
Code:
Label6.Text = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) +
VBSpace +
NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
-
Jan 11th, 2018, 11:43 AM
#4
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
this is what i want it to look like here is a pic of my windows drive same size as my apps above but only problem is
these (HarddriveAvailableFreeSpace HarddriveTotalFreeSpace) are the same size for some reason i need the progress bars max to be HarddriveTotalFreeSpace the progess bars value to the HarddriveAvailableFreeSpace so my progessbar will look like the image from my computer below but they both = the same size for some reason why my progress bar is full in my apps pic above
HarddriveAvailableFreeSpace
HarddriveTotalFreeSpace
-
Jan 11th, 2018, 11:47 AM
#5
Re: Harddrive - Format Free Space
Wouldn't TotalSize (https://msdn.microsoft.com/en-us/lib...=vs.110).aspx) be a better option for the drive size rather than TotalFreeSpace?
-
Jan 11th, 2018, 11:48 AM
#6
Re: Harddrive - Format Free Space
Both of the functions in your posted code get Free Space. You need a function that gets the total size.
You need something like this:
Code:
Public Function HarddriveTotalSize() As Long
For Each curDrive As DriveInfo In My.Computer.FileSystem.Drives
If curDrive.DriveType = DriveType.Fixed Then
Dim theSize As Long = curDrive.TotalSize
Return theSize
End If
Next
End Function
-
Jan 11th, 2018, 11:52 AM
#7
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
I tried this so this will be easier to show ya
Code:
ProgressBar1.Maximum = NthField(FormatSize(HarddriveTotalFreeSpace), ".", 1)
ProgressBar1.Value = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1)
Dim TotalFreeSize As String = NthField(FormatSize(HarddriveTotalFreeSpace), ".", 1) + VBSpace + NthField(FormatSize(HarddriveTotalFreeSpace), " ", 2)
Dim AvailableFreeSize As String = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) + VBSpace + NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
Label6.Text = AvailableFreeSize + " free of " + TotalFreeSize
-
Jan 11th, 2018, 12:19 PM
#8
Re: Harddrive - Format Free Space
You are displaying very different information to what Windows is showing... Windows is showing "Used Space" out of "Total Size (used + unused)", whereas you are showing two variants of "Unused space".
What you want is something like:
Code:
ProgressBar1.Maximum = HarddriveTotalSize
ProgressBar1.Value = (HarddriveTotalSize - HarddriveAvailableFreeSpace)
I assume that Windows bases the percentage on AvailableFreeSpace rather than TotalFreeSpace, because the difference between them is space that cannot be used anyway.
-
Jan 11th, 2018, 12:24 PM
#9
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
PERFECT si the geek thanks base
Code:
Dim ProgessFreeSpace As String = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1)
Dim ProgessTotalSize As String = NthField(FormatSize(HarddriveTotalSize), ".", 1)
ProgressBar1.Maximum = Val(ProgessTotalSize)
ProgressBar1.Value = Val(ProgessTotalSize - ProgessFreeSpace)
Dim DriveFreeSpace As String = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) + VBSpace + NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
Dim DriveTotalSize As String = NthField(FormatSize(HarddriveTotalSize), ".", 1) + VBSpace + NthField(FormatSize(HarddriveTotalSize), " ", 2)
Label6.Text = DriveFreeSpace + " free of " + DriveTotalSize
this is what it looks like now how should look
-
Jan 11th, 2018, 12:36 PM
#10
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
i use all the nthfields to split and remove the dots from the size to get a numeric only value for the progress bar otherwise it will look like this below
Code:
Dim ProgessFreeSpace As String = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1)
Dim ProgessTotalSize As String = NthField(FormatSize(HarddriveTotalSize), ".", 1)
ProgressBar1.Maximum = Val(ProgessTotalSize)
ProgressBar1.Value = Val(ProgessTotalSize - ProgessFreeSpace)
Dim DriveFreeSpace As String = NthField(FormatSize(HarddriveAvailableFreeSpace), ".", 1) + VBSpace + NthField(FormatSize(HarddriveAvailableFreeSpace), " ", 2)
Dim DriveTotalSize As String = NthField(FormatSize(HarddriveTotalSize), ".", 1) + VBSpace + NthField(FormatSize(HarddriveTotalSize), " ", 2)
Label6.Text = FormatSize(HarddriveAvailableFreeSpace()) + " free of " + FormatSize(HarddriveTotalSize())
-
Jan 11th, 2018, 12:43 PM
#11
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
is there a way to get the progress bar glow highlight thing to stop blinking tried to set it to blocks but still glow blink updates?
-
Jan 11th, 2018, 01:07 PM
#12
Re: Harddrive - Format Free Space
One way is to make a new control instead of using the ProgressBar.
A simple way to do it is to have one Panel (for the grey "total" part) set to the same size as you have the ProgressBar, then put another Panel inside it (pick a colour, perhaps a shade of green or blue), and set the size of the inner one based on the relative sizes, eg:
Code:
Dim ProgessFreeSpace As Long = HarddriveAvailableFreeSpace
Dim ProgessTotalSize As Long = HarddriveTotalSize
PanelSizeUsed.Width = PanelSizeTotal.Width * ((ProgessTotalSize - ProgessFreeSpace) / ProgessTotalSize)
(not sure I've got the last line right, but it's vaguely the concept!)
-
Jan 11th, 2018, 03:57 PM
#13
Re: Harddrive - Format Free Space
Originally Posted by sinner0636
is there a way to get the progress bar glow highlight thing to stop blinking tried to set it to blocks but still glow blink updates?
Who is writing your file app? vbforums or yourself. It's One question per thread.
"For Each curDrive As DriveInfo In My.Computer.FileSystem.Drives"
Either use the Io.DriveInfo class or use the my.compter namespace. Do you look at any documentation from Microsoft that has examples and documentation of everything you have asked? You are running before you can walk. I don't believe you understand any of the code you post bud.
First, you need to turn option strict ON. Learn the difference between functions and subs.
On Error Resume Next
This is VB6 legacy code and should not be used in Visual Basic.
-
Jan 12th, 2018, 10:03 AM
#14
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
yeah i see what you mean srry not that great yet with vb.net i have only been using it for few weeks i used to code for years in realbasic here is one my apps in rb i coded few years ago can make a file scanner or a remote admin or keylogger remote chat really easy in rb but i am converting to vb.net rb is really slow code plus its buggy
http://https://sourceforge.net/projects/pcmicroalarm/
was reading about sub functions how sub don't return only functions do and error handling in vb.net after u said this i see now what your saying about turning on option explicit otherwise your code will continue to run with errors proper way to code the the nthfield code i used to use this in rb/xojo now were i converted it from
only thing is rb is already coded for you why so easy where as vb you have to do the codes yourself
https://docs.xojo.com/index.php/NthField
i am supersized my nthfield code ran in vb.net lol i copied and pasted it from my older vb6 program i used before going to start looking more at the vb.net language reference
look better?
Code:
Public Function NthField(ByVal expression As String, ByVal separator As String, ByVal fieldNum As Long) As String
Dim fields() As String
fields = Split(expression, separator, , vbTextCompare)
Return fields(fieldNum - 1)
End Function
Code:
Public Function HarddriveTotalSize() As Long
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
If d.IsReady And d.DriveType = DriveType.Fixed Then
Dim theSize As Long = d.TotalSize
Return theSize
End If
Next
End Function
Last edited by sinner0636; Jan 12th, 2018 at 10:38 AM.
-
Jan 12th, 2018, 10:52 AM
#15
Re: Harddrive - Format Free Space
Those look good, but note that Split is a VB-specific function... That doesn't mean it is bad or the wrong thing to use (it gives benefits over the the standard .Net version), just that converting to C# or another language will be slightly harder.
Note that this section of your code:
Code:
Public Function HarddriveTotalSize() As Long
Dim allDrives() As DriveInfo = DriveInfo.GetDrives()
Dim d As DriveInfo
For Each d In allDrives
..can be made shorter with no change in behaviour:
Code:
Public Function HarddriveTotalSize() As Long
For Each d As DriveInfo In DriveInfo.GetDrives()
However, which you use is a matter of opinion (eg: having the allDrives variable makes debugging easier).
Also note that "Option Explicit" and "Option Strict" are different things, and both have benefits in eliminating problems when turned On. Option Explicit forces you to declare your variables (which reduces the chance of accidentally using the wrong variable, especially in terms of typos), and Option Strict forces you to to use data types properly (eg: you can't store the text of a textbox directly to an Integer, because it is possible for the user to enter something other than an integer). While Option Strict means writing a bit more code, that code is safer and runs faster.
-
Jan 12th, 2018, 11:22 AM
#16
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
yep i like Option Strict a lot better seems i just added it and caught a bunch of stuff like this for example i did not use 'As String'
i see a lot of people half way coding like this when googling for examples and stuff
Public VBBlankSpace = Nothing
-
Jan 12th, 2018, 12:05 PM
#17
Re: Harddrive - Format Free Space
That particular example shows that Strict is off, but making a minor change alters that:
Code:
Public VBBlankSpace = " "
This may seem to not have changed much, but in this case the data type might actually be specified... if you have Option Infer On then the data type will be automatically set to the same as the value given (so in this case, " " is a String, so with Option Infer On the variable VBBlankSpace will be declared as a String).
Out of the three, Option Infer is easily the most debatable, because while it saves effort in typing, it does remove clarity of what the data types are (you can still find out, but it takes more effort... and generally isn't a good idea in examples/tutorials).
-
Jan 12th, 2018, 01:32 PM
#18
Re: Harddrive - Format Free Space
Yeah, Option Infer is a really weirdo thing. It follows a pattern that gets made fun of in a lot of social circles, but is sadly true. The joke always goes:
Beginner: "Oh, I'm going to solve this problem by <thing that is dangerous.>"
Novice: "Oh, you solve this problem with <very complex way to avoid danger.>"
Expert: "Oh, I'm going to solve this problem by <thing that is dangerous."
There's some truth, but only experts get what's going on there.
Stuff is "dangerous" in programming because it involves some mental shortcuts. Beginners and novices don't have enough experience to understand when the shortcuts are too dangerous to take. So it's better to teach people "don't use the shortcut" until they can start saying things like, "Yeah, OK, but what if I'm in <safe case>, and can guarantee <risk> never happens? The shortcut isn't so bad then."
So beginners are the people who get mauled by wolves in the forest because they didn't know wolves were in the forest. Novices are the people who got bit by a wolf one time so they go around the forest. Experts are the people who won't go into any old forest, but know THIS forest's wolves are only active at dusk so they travel through the forest at noon.
How's that work with Option Infer?
Well, newbies tend to write code like this:
Code:
Dim this_variable_is_a_string_NumberOfApples = 3
I don't know why. It seems counterproductive. But that's what I see when I see something like this:
Code:
Dim intApples = Console.ReadLine()
Dim something = intApples * 10
Alarm bells go off in my expert head. ReadLine() returns a String, but it's going in a variable named "intApples"? Then it gets multiplied by 10? And I start talking like a Valley girl?
Option Strict catches that, and forces you to write this:
Code:
Dim intApples As Integer = CInt(Console.ReadLine())
Dim something As Integer = intApples * 10
If you do that for a few years, you start to get used to what's what, and experts tend to use Option Infer to write it like this:
Code:
Dim userInput = Console.ReadLine()
Dim numberOfApples = CInt(userInput)
Dim something = numberOfApples * 10
'userInput' must be a String. 'numberOfApples' must be an Integer. 'something' [b]must be[/i] another Integer. So the expert doesn't feel a need to be quite so verbose. This is especially useful when working with LINQ, where you might have a type like 'IList(Of IGrouping(Of String, List(Of Something)))' but a LINQ expert can just automatically know that. It's very hard to explain because it happens via intuition, and you develop that over years!
But don't be fooled, Option Infer doesn't cancel Option Strict. This fails with Option Strict on:
Code:
Dim userInput = Console.ReadLine()
Dim something = userInput * 10
Even though I didn't say "As String", it as inferred. Since 'userInput' is a String, I can't multiply it by 10 and the compiler complains.
I would never, ever turn off Option Strict in normal code. I do use Option Infer, but in some situations when the code is complex I STILL use "As whatever" to help myself remember what I'm doing.
My rule for examples is to only use Option Infer-style declarations if it's blatantly obvious what is going on. So I don't mind typing:
Code:
Dim numberOfApples = 10
But I would use the "As <Type>" for (in an example):
Code:
Dim rottenApples As IEnumerable(Of Apple) = _apples.Where(Function(apple As Apple) apple.IsRotten)
I don't follow it 100%. Sometimes I make a guess based on how skilled the reader seems.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jan 13th, 2018, 01:04 PM
#19
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
so do you guys when you code do u like to keep one of the error catching options on or do you keep all of them on and if you keep all of them on will they suggest the errors wrong?
-
Jan 13th, 2018, 01:44 PM
#20
Re: Harddrive - Format Free Space
Most people agree that you should always use Option Explicit and Option Strict, apart from a very few cases where they are impossible... which are cases you may never encounter (many people don't).
When it comes to Option Infer opinion is divided, but a general guideline is that if you aren't certain what data types will be used without checking, then you aren't ready for it (yet). Even when you are ready, you should decide for yourself if it is a good idea or not.
-
Jan 13th, 2018, 04:03 PM
#21
Re: Harddrive - Format Free Space
Option Explicit Off says "If I don't tell you about a variable I want you to make one automatically". It's really dumb to turn it off and everyone agrees. It lets you make mistakes like this:
Code:
Dim myName As String = "Sitten Spynne"
Console.WriteLine(myNme)
That will compile and run, but print a blank. Yuck.
Option Strict turns off a ton of things people didn't like about VB6. Its behavior varies based on how Option Infer is set.
If Option Strict is off:
- You don't have to specify a type for any variable. The type depends on Option Infer.
- Late binding is enabled. This means if the compiler doesn't know for sure if what a line does is safe, it lets it happen anyway and the program might crash at runtime.
- Implicit unsafe conversions are allowed, such as String->Integer. This works if they work, and fails at runtime if they don't.
If Option Strict is on:
- You have to specify a type for every variable if Option Infer is off. If Option Infer is On, the compiler will try to guess a type for the variable. If it can't guess, you MUST specify a type.
- Late binding is NOT enabled.
- Implicit unsafe conversions are never allowed, to help you realize they need validation.
Almost all professional developers prefer Option Strict On, because it helps them catch simple mistakes. There are rare situations (like working with COM interop) where late binding is very useful and experts will turn it off. In THAT FILE only.
Option Infer only has a big impact on that first rule for Option Strict.
If Option Infer is off AND Option Strict is Off, any variable with an undeclared type is Object. In all other cases, the compiler looks at the right-hand side of the expression. If that expression has an unambiguous type, that type will be the type of the variable.
So:
Code:
Dim firstName = "Sitten Spynne"
If Option Strict and Option Infer are OFF, that variable will be an Object. If Option Infer is ON, that variable will be a String no matter what Option Strict is set to. If Option Strict is On and Option Infer is Off, this line is an error because it needs a type.
This is why Option Strict is important:
Code:
Dim firstName = 10
Dim firstLetter = firstName.Substring(0, 1)
If both Option Strict and Option Infer are off, firstName and firstLetter are Object. Object doesn't have a .Substring(), but since late binding is enabled, it will compile. When you run the code, it fails. Probably. VB might do an Integer -> String conversion, it's hard to know without memorizing the spec.
If Option Strict is on, this code won't compile whether or not Option Infer is on.
If Option Infer is On, firstName is an Integer, and since Integer doesn't have a .Substring() and late binding is disabled by Option Strict, it fails to compile.
If Option Infer is Off, the code won't compile because the variables need a type.
So with Option Strict On:
If Option Infer is On, you need to make sure the correct type can be inferred.
Code:
Dim firstName = "10"
' -OR-
Dim firstName = 10.ToString()
Now firstName can unambiguously be a String, which has a .Substring().
If Option Infer is Off, you need to assign types to the variables AND make sure they match:
Code:
Dim firstName As String = "10"
' -OR-
Dim firstName As String = 10.ToString()
Dim firstLetter As String = firstName.Substring(0, 1)
Some people argue Option Infer makes code more confusing. Others argue it makes code less cluttered. I think it's a personal decision.
Personally, I think the code that looks confusing when Option Infer is on tends to be written in a way I wouldn't even choose if Option Infer is Off. I use Option Infer by choice.
And I never, ever turn off Option Explicit. And I only turn off Option Strict if I have a very specific reason.
This answer is wrong. You should be using TableAdapter and Dictionaries instead.
-
Jan 15th, 2018, 01:20 PM
#22
Thread Starter
Addicted Member
Re: Harddrive - Format Free Space
ty for the advice you guys are awesome i am definitely sticking with Option Strict On Option Explicit On for now on in all my programs
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|