Results 1 to 15 of 15

Thread: Application.StartupPath is not working vb.net

  1. #1

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Application.StartupPath is not working vb.net

    Dear All

    I used the
    Code:
    Dim Path As String = Application.StartupPath & "\Main_Master_VB.xls"
    in vb.net Application and it worked very well but when I used it in another vb.net application with the same references it is not working it gives the following Error:

    Reference to a non-shared member requires an object reference, I do not know what is the missing reference ?

    Thanks your support

    Moheb Labib
    Last edited by meho2020; Feb 25th, 2021 at 02:45 AM.

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Application.StartupPath is not working vb.net

    Hi,

    try it this way

    Code:
     Dim Path As String = Application.StartupPath & "\..\Main_Master_VB.xls"
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Re: Application.StartupPath is not working vb.net

    Thanks ChrisE But it did not change anything Still the same error

  4. #4
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Application.StartupPath is not working vb.net

    is the Excelfile in the Bin folder ?
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by ChrisE View Post
    is the Excelfile in the Bin folder ?
    You're trying to solve the wrong problem. The file path is irrelevant. Look at the error message.

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

    Re: Application.StartupPath is not working vb.net

    I'm going to copy/paste my responses from SO:

    Application is the System.Windows.Forms.Application class, so it is obviously specific to Windows Forms. If you're not in a Windows Forms project then there is no such class and so no such property.

    Given that System.Windows.Forms.Application.StartupPath is a Shared member, if you are in a WinForms project then there's something else at play that is causing a name clash, e.g. the placement of the code or the naming of a class in your project.

  7. #7

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Re: Application.StartupPath is not working vb.net

    Dear ChrisE

    I found that it is working when I remove the

    Code:
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    and Keep only
    Code:
    Imports System.IO
    So, What do this means?

    Thanks

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

    Re: Application.StartupPath is not working vb.net

    For good measure, here's some OT advice:

    Don't use string concatenation to build file and folder paths from parts.
    vb.net Code:
    1. Dim filePath = Path.Combine(Application.StartupPath, "Main_Master_VB.xls")
    That method will always get the separators right, whether either, both or neither of the parts have separators or not. Note the more descriptive variable name, so as to avoid a name clash with the System.IO.Path class. It's also just better.

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

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by meho2020 View Post
    Dear ChrisE

    I found that it is working when I remove the

    Code:
    Imports Microsoft.Office.Interop
    Imports Microsoft.Office.Interop.Excel
    and Keep only
    Code:
    Imports System.IO
    So, What do this means?

    Thanks
    It means what I said it means at SO and copied above: you have a name clash. Application is presumably being interpreted as an Excel type. If you qualify the type to disambiguate the name then it will work as expected:
    vb.net Code:
    1. Dim filePath = Path.Combine(Windows.Forms.Application.StartupPath, "Main_Master_VB.xls")

  10. #10

    Thread Starter
    Banned
    Join Date
    Apr 2020
    Location
    https://t.me/pump_upp
    Posts
    61

    Re: Application.StartupPath is not working vb.net

    Thanks but it is not working , However I removed the
    Code:
    Imports Microsoft.Office.Interop.Excel
    and it worked

    Thanks

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

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by meho2020 View Post
    Thanks but it is not working
    Then you did it wrong.

  12. #12
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by meho2020 View Post
    Thanks but it is not working , However I removed the
    Code:
    Imports Microsoft.Office.Interop.Excel
    and it worked

    Thanks


    removing a ref. and it works, never heard that one before
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  13. #13
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by jmcilhinney View Post
    You're trying to solve the wrong problem. The file path is irrelevant. Look at the error message.
    overread that one
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by ChrisE View Post


    removing a ref. and it works, never heard that one before
    System.Windows.Forms is imported at the project level. File-level imports take precedence over project-level imports. If you import Microsoft.Office.Interop.Excel at the file level then Application is interpreted as Microsoft.Office.Interop.Excel.Application rather than System.Windows.Forms.Application.

  15. #15
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Application.StartupPath is not working vb.net

    Quote Originally Posted by jmcilhinney View Post
    System.Windows.Forms is imported at the project level. File-level imports take precedence over project-level imports. If you import Microsoft.Office.Interop.Excel at the file level then Application is interpreted as Microsoft.Office.Interop.Excel.Application rather than System.Windows.Forms.Application.
    didn't know that, learn something every day at my old age
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

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