-
Mar 5th, 2021, 12:01 AM
#1
Thread Starter
Member
Empty Path Name Is Not Legal Error help
Hi, I am outputting an exe file and have an option to add an icon. The keyword is "option" but when I don't add an icon, it gives me an error saying "Empty Path Name Is Not Legal"
The following code is what I am using. Mind you the app still spits out the file just fine even with the error. I would just like to somehow suppress/get rid of the error.
Any and all help is much appreciated! Thanks in advance
Code:
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim ofd As New OpenFileDialog
ofd.Title = "Select an icon"
ofd.Filter = "Icon (*.ico)|*.ico"
If ofd.ShowDialog <> vbOK Then
Exit Sub
End If
TextBox4.Text = ofd.FileName
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.ImageLocation = ofd.FileName
End Sub
-
Mar 5th, 2021, 12:19 AM
#2
Re: Empty Path Name Is Not Legal Error help
Firstly, ShowDialog returns a DialogResult value so you should be comparing the result to a value of that type. You should turn Option Strict On and start using the correct data types.
As for the issue, please provide a FULL and CLEAR explanation of it. You say "it" but what exactly is "it"? If it is in that specific code snippet, where in that specific code snippet? Does it occur when you set the ImageLocation property? If so, say so. If not, tell us where it actually does occur.
-
Mar 5th, 2021, 12:37 AM
#3
Thread Starter
Member
Re: Empty Path Name Is Not Legal Error help
My apologies, the app gives me the error.
I gave you the incorrect code.
The error points to the follow snippet of code
Code:
Public Shared Function FromFile(ByVal filename As String) As IconFile
Dim instance As New IconFile
Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename)
Dim pinnedBytes = GCHandle.Alloc(fileBytes, GCHandleType.Pinned)
instance.iconDir = DirectCast(Marshal.PtrToStructure(pinnedBytes.AddrOfPinnedObject, GetType(ICONDIR)), ICONDIR)
instance.iconEntry = New ICONDIRENTRY(instance.iconDir.Count - 1) {}
instance.iconImage = New Byte(instance.iconDir.Count - 1)() {}
Dim offset = Marshal.SizeOf(instance.iconDir)
Dim iconDirEntryType = GetType(ICONDIRENTRY)
Dim size = Marshal.SizeOf(iconDirEntryType)
For i = 0 To instance.iconDir.Count - 1
Dim entry = DirectCast(Marshal.PtrToStructure(New IntPtr(pinnedBytes.AddrOfPinnedObject.ToInt64 + offset), iconDirEntryType), ICONDIRENTRY)
instance.iconEntry(i) = entry
instance.iconImage(i) = New Byte(entry.BytesInRes - 1) {}
Buffer.BlockCopy(fileBytes, entry.ImageOffset, instance.iconImage(i), 0, entry.BytesInRes)
offset += size
Next
pinnedBytes.Free()
Return instance
End Function
specifically the following line
Code:
Dim fileBytes() As Byte = IO.File.ReadAllBytes(filename)
Last edited by Yourmomsfire; Mar 5th, 2021 at 12:47 AM.
-
Mar 5th, 2021, 12:51 AM
#4
Re: Empty Path Name Is Not Legal Error help
What is an example of the value for filename
-
Mar 5th, 2021, 12:59 AM
#5
Re: Empty Path Name Is Not Legal Error help
I would question why you're even calling that method at all if no file has been selected. In that case, presumably filename must either be Nothing or String.Empty, neither of which can possibly do anything useful when passed to File.ReadAllBytes. If you're not going to avoid calling the method at all when you know it can't do anything useful, at least validate the data in the method, i.e. test whether filename actually contains anything and possibly whether that something is a valid file path too, then return Nothing if it doesn't/isn't.
-
Mar 5th, 2021, 01:26 AM
#6
Thread Starter
Member
Re: Empty Path Name Is Not Legal Error help
I can't figure it out, is there a way to just suppress the damn error?
-
Mar 5th, 2021, 02:30 AM
#7
Re: Empty Path Name Is Not Legal Error help
Put a breakpoint at the line of code, run it, and watch the filename to retrieve the value and post back the result.
-
Mar 5th, 2021, 02:37 AM
#8
Re: Empty Path Name Is Not Legal Error help
So what you’re saying is...
Code:
Public Shared Function FromFile(ByVal filename As String) As IconFile
Gives you an error, when you call it, passing an empty String for filename???
The second line in your FromFile function...
Code:
IO.File.ReadAllBytes(filename)
ReadAllBytes requires a valid filename. It cannot be “”
If you don’t want an icon, why even call FromFile???
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 5th, 2021, 07:40 AM
#9
Re: Empty Path Name Is Not Legal Error help
Originally Posted by Yourmomsfire
I can't figure it out, is there a way to just suppress the damn error?
Personally, I'm not going to spend time trying to help someone with that attitude. You can't figure out how to check whether a variable has a value? You're just not trying.
-
Mar 5th, 2021, 09:27 AM
#10
Re: Empty Path Name Is Not Legal Error help
I can understand your frustration when something incredibly simple isn't working like you want it to and you can't figure out why, but try to keep your composure. Remember, you can catch more bees with honey than with vinegar.
First off, like JMcIlhinney suggested, you need to turn on Option Strict. I will shamelessly plug my own website that discusses Option Strict and how to turn it on: https://vblessons.com/lessons.html#/1/2
Secondly, OpenFileDialog implements IDisposable. Anytime you're working with a class that does so, you should wrap the declaration in Using statements so that it gets cleaned up properly by the garbage collection (documentation).
Thirdly, you should be using PictureBox.Load over specifying the ImageLocation. I cannot remember why off-hand, but if JMcIlhinney is willing to elaborate on it, I definitely remember him mentioning this to another member in a separate thread.
Take a look at this example:
Code:
Using ofd As New OpenFileDialog()
With ofd
.Title = "Select an icon"
.Filter = "Icon (*.ico)|*.ico"
If (.ShowDialog() = DialogResult.Ok) Then
TextBox4.Text = .FileName
PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
PictureBox1.Load(.FileName)
End If
End With
End Using
Now for your root issue. Since specifying the icon is optional, check if there is a value in your TextBox that represents the file location. If it is empty, then don't call your FromFile method:
Code:
'...
If (Not String.IsNullOrWhitespace(TextBox4.Text)) Then
' do something with FromFile(TextBox4.Text)
End If
'...
-
Mar 5th, 2021, 09:47 AM
#11
Re: Empty Path Name Is Not Legal Error help
Originally Posted by dday9
Code:
'...
If (Not String.IsNullOrWhitespace(TextBox4.Text)) Then
' do something with FromFile(TextBox4.Text)
End If
'...
You need to test it’s a valid file name too...
Code:
If (Not String.IsNullOrWhitespace(TextBox4.Text)) Then
If IO.Fike.Exists(TextBox4.Text) Then
' do something with FromFile(TextBox4.Text)
End If
End If
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Mar 5th, 2021, 09:52 AM
#12
Hyperactive Member
Re: Empty Path Name Is Not Legal Error help
btw, paul, small typo:
Code:
If (Not String.IsNullOrWhitespace(TextBox4.Text)) Then
If IO.File.Exists(TextBox4.Text) Then
' do something with FromFile(TextBox4.Text)
End If
End If
Please dont forget to add good reputation if my advices were useful for you.
How? Under this post there is "RATE THIS POST" button. Click on it.
-
Mar 5th, 2021, 10:13 AM
#13
Re: Empty Path Name Is Not Legal Error help
Originally Posted by .paul.
You need to test it’s a valid file name too...
Yeah, I was making the assumption that the filename wouldn't change after using the OpenFileDialog, which is not a good assumption.
Good catch.
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
|