Click here to Skip to main content
15,910,878 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello im kind of newbie in VB however ive decided to build tough program that will teach me alot, Unfortunetly I'm stuck >~<
here's a part of my code:

    Try
        Dim foundFile As String = My.Computer.FileSystem.GetFiles(filename).ToString()
        Dim check As String = System.IO.Path.GetExtension(foundFile)
        Return check
    Catch ex As ArgumentException
    End Try
End Function

this part is responsable of saparting the extension out of the File's name

At this part:
VB
Dim foundFile As String = My.Computer.FileSystem.GetFiles(filename).ToString()
Dim check As String = System.IO.Path.GetExtension(foundFile)


I'm getting IOException was unhendaled by user code:

Thank you in advance, tsahi.

[edit]Spurious tags removed - OriginalGriff[/edit]
Posted
Updated 13-Sep-11 4:39am
v2

 
Share this answer
 
If you look at the documentation (MSDN[^]) you will see:
C#
The following conditions may cause an exception:

The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\) (ArgumentException).

The path is not valid because it is Nothing (ArgumentNullException).

directory does not exist (DirectoryNotFoundException).

directory points to an existing file (IOException).

The path exceeds the system-defined maximum length (PathTooLongException).

A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).

The user lacks necessary permissions to view the path (SecurityException).

The user lacks necessary permissions (UnauthorizedAccessException).
Since you are getting an IOException, the problem becomes pretty obvious.

Solution: Don't give it a file name when it expects just the path...
 
Share this answer
 
GetFiles(path) returns all files within the path you supplied as an array of string values. ToString() will return Array.ToString and not the filename you want.

Just use the following to get what you want :
VB
  ...
  Dim check As String = System.IO.Path.GetExtension(filename)
  Return check
End Function
 
Share this answer
 
Function GetFormat(ByVal filePath As String) As Integer
      Try
          Dim foundFile As String = My.Computer.FileSystem.GetFiles(filepath).ToString()
          Dim check As String = System.IO.Path.GetExtension(foundFile)
          Return check
      Catch ex As ArgumentException
      End Try
  End Function


now filePath gets a genuine file path.

filepath = "C:\Users\Tsahi\Desktop\IMG_0198.JPG"


I've used the command:
My.Computer.FileSystem.GetFileInfo(filePath).Exists

to verify it
VB
<pre lang="vb">
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900