Absurd error GetInvalidPathChars - System.InvalidCastException

DJG 26 Reputation points
2022-12-23T17:40:45.543+00:00

Hi,
executing this code i get this error

Dim dir = "C:\Pippo\conversations\conversation\"
Dim CheckDir As Char = System.IO.Path.GetInvalidPathChars(dir)
Dim data() As Byte = File.ReadAllBytes("C:\Ferie\2020_0908_155849_185.jpg")
File.WriteAllBytes(dir & "2020_0908_155849_185A.jpg", data)

System.InvalidCastException
HResult=0x80004002
Messaggio=Cast not valid from string "C:\Pippo\conversations\conversat" to 'Integer' type.
Origine=Microsoft.VisualBasic
Stack analysis:
in Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value)
in ProvaPath.Form1.Button1_Click(Object sender, EventArgs e) in C:\Progetti\ProvaPath\ProvaPath\Form1.vb: riga 9
in System.Windows.Forms.Control.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnClick(EventArgs e)
in System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
in System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
in System.Windows.Forms.Control.WndProc(Message& m)
in System.Windows.Forms.ButtonBase.WndProc(Message& m)
in System.Windows.Forms.Button.WndProc(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
in System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
in System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
in System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
in System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
in System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
in Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
in ProvaPath.My.MyApplication.Main(String[] Args) in : riga 83

This exception was originally thrown in the following call stack:
Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(string, System.Globalization.NumberFormatInfo)
Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(string)

Eccezione interna 1:
FormatException: Incorrect input string format.

How is it possible?

Thanks.

Developer technologies | VB
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. DJG 26 Reputation points
    2022-12-26T09:40:15.813+00:00

    Hi cooldadtx,
    perhaps i've found the problem, executing your last suggested test, here below the bytes resulting from filename = "0_D_1_6.att", see the last one...

    	(0)	48	Byte  
    	(1)	95	Byte  
    	(2)	68	Byte  
    	(3)	95	Byte  
    	(4)	49	Byte  
    	(5)	95	Byte  
    	(6)	54	Byte  
    	(7)	46	Byte  
    	(8)	97	Byte  
    	(9)	116	Byte  
    	(10)	116	Byte  
    	(11)	0	Byte  
    

    Thanks

    1 person found this answer helpful.

  2. Michael Taylor 60,326 Reputation points
    2022-12-23T18:20:14.48+00:00

    GetInvalidPathChars returns an array of characters that are considered invalid as part of a file path. As such the method has no parameters and returns an array. You are passing an argument to the method and taking the return value and storing it into a single char.

    So the question becomes why VB is allowing you to pass an argument to a method that has none. This is a backwards compatibility thing. In VB (prior to .NET) you called subroutines without parens. So GetInvalidPathChars(dir) would have been GetInvalidPathChars dir. For whatever reason the compiler rewrites the code to the invalid, but equivalent, GetInvalidPathChars()(dir). Since the method is returning a character array it tries to index into the array to get the value but it is using a string and not an int and therefore you get the runtime error.

    0 comments No comments

  3. DJG 26 Reputation points
    2022-12-24T08:27:45.827+00:00

    Hi cooldadtx,
    thank for the answer.
    Now i've tried to remove the As Char from Dim CheckDir As Char = System.IO.Path.GetInvalidPathChars(dir)
    and so now i've Dim CheckDir = System.IO.Path.GetInvalidPathChars(dir).
    Executing the code now i obtain

    'Invalid cast from string "C:\Pippo\conversations\conversat" to type 'Integer'.'

    and before this i've received on time chrW(15) not valid (where is chrW(15) in this string ?)

    I'm a bit confused


  4. DJG 26 Reputation points
    2022-12-24T20:41:52.113+00:00

    Here is the code.

    273882-image.png

    273806-image.png

    caratteri non validi nel percorso = invalid characters in the path


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.