-
Feb 25th, 2021, 02:38 AM
#1
Thread Starter
Banned
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.
-
Feb 25th, 2021, 03:14 AM
#2
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.
-
Feb 25th, 2021, 03:17 AM
#3
Thread Starter
Banned
Re: Application.StartupPath is not working vb.net
Thanks ChrisE But it did not change anything Still the same error
-
Feb 25th, 2021, 03:18 AM
#4
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.
-
Feb 25th, 2021, 03:27 AM
#5
Re: Application.StartupPath is not working vb.net
Originally Posted by ChrisE
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.
-
Feb 25th, 2021, 03:29 AM
#6
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.
-
Feb 25th, 2021, 03:29 AM
#7
Thread Starter
Banned
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
So, What do this means?
Thanks
-
Feb 25th, 2021, 03:29 AM
#8
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:
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.
-
Feb 25th, 2021, 03:31 AM
#9
Re: Application.StartupPath is not working vb.net
Originally Posted by meho2020
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
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:
Dim filePath = Path.Combine(Windows.Forms.Application.StartupPath, "Main_Master_VB.xls")
-
Feb 25th, 2021, 03:34 AM
#10
Thread Starter
Banned
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
-
Feb 25th, 2021, 03:38 AM
#11
Re: Application.StartupPath is not working vb.net
Originally Posted by meho2020
Thanks but it is not working
Then you did it wrong.
-
Feb 25th, 2021, 03:44 AM
#12
Re: Application.StartupPath is not working vb.net
Originally Posted by meho2020
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.
-
Feb 25th, 2021, 03:53 AM
#13
Re: Application.StartupPath is not working vb.net
Originally Posted by jmcilhinney
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.
-
Feb 25th, 2021, 06:46 AM
#14
Re: Application.StartupPath is not working vb.net
Originally Posted by ChrisE
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.
-
Feb 25th, 2021, 07:22 AM
#15
Re: Application.StartupPath is not working vb.net
Originally Posted by jmcilhinney
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.
-
Jul 23rd, 2024, 02:54 AM
#16
New Member
Re: Application.StartupPath is not working vb.net
Check to see if one of your imports are OverRiding Application
Imports System.Net.Mime.MediaTypeNames
This redefined Application so that Application.StartupPath or Application.ExecutablePath wasn't recognized.
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
|