|
-
Nov 29th, 2016, 11:12 AM
#1
Thread Starter
Member
Counting Problem
I'm creating a program that counts files, but the special environments folders won't pick up. This program searches C:/Windows/Temp for files and counts them. But I keep getting the error
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll
Additional information: Could not find a part of the path 'C:\Users\Jimmy\Desktop\work\WindowsApplication5\WindowsApplication5\bin\Debug\36Temp'.
It shouldn't be pointing to "C:\Users\Jimmy\Desktop\Work\etc.." It should be pointing to "C:\Windows\Temp\". I've also tested this script with the "Environment.SpecialFolder.UserProfile" Environment and when I search folders in my user profile it points back to "C:\Users\Jimmy\Desktop\Work\etc.." I'm soo confused
Can someone please help? It should work I'm not understanding.
Code:
Option Explicit On
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim a1 As Integer = IO.Directory.GetFiles(Environment.SpecialFolder.Windows & "Temp/", "*.*", SearchOption.AllDirectories).Length
MessageBox.Show(String.Format("Total Files: {0}", a1), "Total Files", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
End Class
-
Nov 29th, 2016, 11:23 AM
#2
Re: Counting Problem
What happens when you try to use the SpecialDirectories.Temp property?
-
Nov 29th, 2016, 11:46 AM
#3
Thread Starter
Member
Re: Counting Problem
It seems to work fine
Code:
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim counter1 As Integer = My.Computer.FileSystem.SpecialDirectories.Temp.Count
MsgBox("Number of files is : " & counter1)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
But why isn't UserProfile and Windows Property Not Working?
-
Nov 29th, 2016, 11:50 AM
#4
Re: Counting Problem
You seem to be using the special folder enumeration incorrectly. This is how it should be used:
Code:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
-
Nov 29th, 2016, 12:07 PM
#5
Thread Starter
Member
Re: Counting Problem
Hey thanks for the help this seems to be the trick to get it to work
Code:
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Windows As String = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
Dim UserProfile As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim counter1 As Integer = Directory.GetFiles(Windows & "\Temp\", "*.*", SearchOption.AllDirectories).Count
Dim counter2 As Integer = Directory.GetFiles(UserProfile & "\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\", "*.*", SearchOption.AllDirectories).Count
MsgBox("Number of files is : " & counter1 + counter2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
-
Nov 29th, 2016, 12:14 PM
#6
Re: Counting Problem
Well you discovered an interesting glitch in VB. I would have thought that Option Strict should flagged the error with the implicit conversion of Environment.SpecialFolder.Windows to a string.
This fails:
Code:
Dim s As String = Environment.SpecialFolder.Windows
but this is ok
Code:
Dim s As String = Environment.SpecialFolder.Windows & ""
So it must slip through the cracks when concatenating.
Thanks for inadvertently making that known to me.
-
Nov 29th, 2016, 12:49 PM
#7
Re: Counting Problem
Whenever you concatenate anything with Option Strict on it implicitly calls the ToString method of whatever it is that you're using. The same occurs with Integers:
Code:
Dim strValue As String = 1 'Gives error because of implicit conversion
Dim newStrValue As String = 1 & "" 'Does not give error anymore because of &
-
Nov 29th, 2016, 01:07 PM
#8
Re: Counting Problem
 Originally Posted by dday9
Whenever you concatenate anything with Option Strict on it implicitly calls the ToString method of whatever it is that you're using. The same occurs with Integers:
Note to self: RTFM 
The & Operator is defined only for String operands, and it always widens its operands to String, regardless of the setting of Option Strict. The & operator is recommended for string concatenation because it is defined exclusively for strings and reduces your chances of generating an unintended conversion.
-
Nov 29th, 2016, 02:42 PM
#9
Thread Starter
Member
Re: Counting Problem
I tested my original code with option strict off and also deleted the option property and still kept getting the same error. Apparently I need to label the Environment.SpecialFolder.Windows (c:/Windows/) and Environment.SpecialFolder.UserProfile (c:/users/currentusername/ ) for the program to execute to the right folder or else it will search for a Windows folder or users folder in the program directory returning an error stating the folder doesn't exist. Kind of weird this happened.. non the less is fixed because of the help of everyone and fooling around with the code so thanks everyone for the help! If I did legitimately found a glitch I'm pumped! LOL! But it should be addressed to Microsoft so maybe they can fix this issue in future products or updates of vb. thanks again
-
Nov 29th, 2016, 03:02 PM
#10
Re: Counting Problem
jm,
No its not a glitch as I admitted to above. It's one of the things VB.Net does to make itself compatible (for better or worse) with prior versions of VB. This particular one while somewhat innocuous in one case, prevented the built-in error checking from flagging your improper use of "Environment.SpecialFolder.Windows" (an Enum value of type Integer).
One other operator that you should be cognizant of is the equality operator for strings. i.e.:
Code:
If FredVar = "Fred" Then
Its behavior is governed by either setting Option Compare to either "Text" or "Binary". It also treats empty strings as being equal to Nothing (null). Therefore, your best bet to make certain that your code behaves as expect when doing string comparisons for equality is to use one of the String.Equals methods.
-
Nov 29th, 2016, 03:13 PM
#11
Thread Starter
Member
Re: Counting Problem
Sorry I must've misread. I was thinking I was awesome for a second lol but I see what you're saying. All in a good days of work of learning I plan on using if statements when I carry on my journey that's for sure. Thanks again
-
Nov 29th, 2016, 03:40 PM
#12
Re: Counting Problem
I thought it would be best to point out an issue... in post #1 this is part of your code:
Code:
Environment.SpecialFolder.Windows & "Temp/"
Assuming Environment.SpecialFolder.Windows returns "C:\Windows", the overall result will be "C:\WindowsTemp/", which has two problems - not only is it missing the \ in between the folder names (thus looks for WindowsTemp instead), but you have / at the end which is not valid, and will presumably cause issues (while Windows will allow you to incorrectly use / in some situations, using a mixture of \ and / is much more likely to fail).
 Originally Posted by jmiller1225
Hey thanks for the help this seems to be the trick to get it to work
Code:
Option Strict On
Option Infer Off
Imports System.IO
Imports System.Collections.Generic
Imports System.Linq
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Windows As String = Environment.GetFolderPath(Environment.SpecialFolder.Windows)
Dim UserProfile As String = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)
Dim counter1 As Integer = Directory.GetFiles(Windows & "\Temp\", "*.*", SearchOption.AllDirectories).Count
Dim counter2 As Integer = Directory.GetFiles(UserProfile & "\AppData\Local\Packages\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\AC\", "*.*", SearchOption.AllDirectories).Count
MsgBox("Number of files is : " & counter1 + counter2)
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
Note that the location of "special" folders can change over time (even on the same computer and version of Windows), and they tend to be named using the language that Windows is defined to use.
As such, attempting to build a path to one (as you did with Windows & "\Temp\" ) is unreliable. If there is a full location available (such as My.Computer.FileSystem.SpecialDirectories.Temp or Environment.SpecialFolder.LocalApplicationData ) then it is best to use it.
Tags for this Thread
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
|