Results 1 to 13 of 13

Thread: Empty Path Name Is Not Legal Error help

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    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

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    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.

  4. #4
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,428

    Re: Empty Path Name Is Not Legal Error help

    What is an example of the value for filename

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    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.

  6. #6

    Thread Starter
    Member
    Join Date
    Feb 2021
    Posts
    41

    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?

  7. #7
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,428

    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.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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???

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Empty Path Name Is Not Legal Error help

    Quote Originally Posted by Yourmomsfire View Post
    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.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    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
    '...
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Empty Path Name Is Not Legal Error help

    Quote Originally Posted by dday9 View Post
    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

  12. #12
    Hyperactive Member
    Join Date
    Oct 2018
    Posts
    276

    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.

  13. #13
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: Empty Path Name Is Not Legal Error help

    Quote Originally Posted by .paul. View Post
    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.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width