-
App.Path in VB.NET
Hi all,
I know VB.NET does not support App.Path, so i found this bit of code:
Code:
Public Function App_Path() As String
Return System.AppDomain.CurrentDomain.BaseDirectory()
End Function
its instructions say copy and Unlike app.path in vb6, this includes the the "\" at the end of the value.
I dont understand what it mean by "\", can anyone advise?
Thankyou
-
In VB6, App.Path only includes a backslash (\) at the end of the path if the app is running in the root directory of the drive. That is, if the app is running in the root directory of drive C:, App.Path returns "C:\". If, on the other hand, the app is running in C:\Program Files\My App, App.Path returns "C:\Program Files\My App" (no backslash at the end). Because of this discrepancy, you need to check for the presence of a trailing backslash before appending a file name to the path.
Your VB.NET App_Path function returns a trailing backslash in all cases -- "C:\" or "C:\Program Files\My App\" -- so you may simply append a file name without checking for a backslash.
Phil Weber
http://www.philweber.com
Please post questions to the forums, where others may benefit.
I do not offer free assistance by e-mail. Thank you!
-
The "real" replacement for App.Path is the following:
Application.ExecutablePath
VB.NET also offers a second option that returns the working directory at the start of the application:
Application.StartupPath
Going through the AppDomain works most of the time, but an AppDomain and an Application are not the same, and in some cases, the code you used could link to something else than the directory you are looking for.
Jacques Bourgeois
JBFI
http://www3.sympatico.ca/jbfi/homeus.htm
-
The replacement Application.ExecutablePath delivers the path of the executable, but not of the dll. If you need the path of the dll that was called as an addin inside of an application (to read config files or whatever) you have to do it this way:
Code:
Module modAppInfos
Public Function App_Path() As String
App_Path = CStr(Nothing)
App_Path = System.Reflection.Assembly.GetExecutingAssembly.GetName().CodeBase
If StrComp(LCase(Left(App_Path, 8)), "file:///") = 0 Then
App_Path = Mid(App_Path, 9) 'file:/// entsorgen
End If
App_Path = StripFileName(App_Path)
End Function
End Module
-
Reply on use of ExecutablePath vs. StartupPath
Just a quick "Thank You" as this answered my question/solved my problem. However, to provide a bit further info, in VB.NET 2002 at least (net 1.1) I found that Application.ExecutablePath was not useful, as it had not only the path, but included the executable file name as well, which I would have had to strip, however, the other suggestion: Application.StartupPath worked perfectly.
Thanks
-
Glad to help.
A little trick that, once again, shows that a lot of things are simpler than they might appear at first sight. You can easily strip the file name from a full name to retrieve only the path:
Code:
App_Path = New System.IO.FileInfo(Application.ExecutablePath).DirectoryName
Jacques Bourgeois
JBFI
http://www3.sympatico.ca/jbfi/homeus.htm
-
Again, thank you JBourgeois,
The technique using ".DirectoryName" was a very nice approach that I was completely unaware of (not suprising as I'm rather new to .NET - I was a "crusty" VB6'er for far too long ~8^D). I don't know how many times I've done the 'ol "instr" searching for the last "\" and fussing the path from a full file spec string.
Best Regards
John Andrews
-
 Originally Posted by JBourgeois
Glad to help.
A little trick that, once again, shows that a lot of things are simpler than they might appear at first sight. You can easily strip the file name from a full name to retrieve only the path:
Code:
App_Path = New System.IO.FileInfo(Application.ExecutablePath).DirectoryName
One quick question! Why are you using 'New' ?
If FileInfo().DirectoryName returns a string and App_Path is a string then why would you need the 'New' in this statement?
-
Thank you! I will check it out.
Similar Threads
-
Replies: 6
Last Post: 11-06-2002, 02:42 PM
-
Replies: 125
Last Post: 10-05-2002, 04:34 PM
-
By Michael Culley in forum .NET
Replies: 6
Last Post: 06-19-2002, 09:11 AM
-
By Bill McCarthy in forum .NET
Replies: 14
Last Post: 04-10-2001, 05:03 AM
-
By David Kroll in forum .NET
Replies: 33
Last Post: 02-13-2001, 10:23 PM
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
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks