-
May 12th, 2003, 05:44 PM
#1
Thread Starter
Let me in ..
VB.NET - Correct method for app.path
Geez, Its been a pain in the freakin ass to get application path in .NET. Here you go buddy's
VB Code:
Private Function GetAppPath() As String
Dim l_intCharPos As Integer = 0, l_intReturnPos As Integer
Dim l_strAppPath As String
l_strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
While (1)
l_intCharPos = InStr(l_intCharPos + 1, l_strAppPath, "\", CompareMethod.Text)
If l_intCharPos = 0 Then
If Right(Mid(l_strAppPath, 1, l_intReturnPos), 1) <> "\" Then
Return Mid(l_strAppPath, 1, l_intReturnPos) & "\"
Else
Return Mid(l_strAppPath, 1, l_intReturnPos)
End If
Exit Function
End If
l_intReturnPos = l_intCharPos
End While
End Function
Last edited by techyspecy; May 12th, 2003 at 05:47 PM.
-
May 14th, 2003, 10:09 AM
#2
Member
You Rule!
Good work!!! Is there anything I miss if I do it like this:
VB Code:
Private Function GetAppPath() As String
Dim i As Integer
Dim strAppPath As String
strAppPath = System.Reflection.Assembly.GetExecutingAssembly.Location()
i = strAppPath.Length - 1
Do Until strAppPath.Substring(i, 1) = "\"
i = i - 1
Loop
strAppPath = strAppPath.Substring(0, i)
Return strAppPath
End Function
-
May 14th, 2003, 10:49 AM
#3
You should use System.IO.Path to strip the filename from the path...e.g.
VB Code:
Return System.IO.Path.GetDirectory(System.Reflection.Assembly.GetExecutingAssembly.Location())
-
Jul 31st, 2003, 10:07 AM
#4
Fanatic Member
This seems to work too, however there might be something improper about it, if there is something wrong with it, I'd like to hear why it shouldn't be done this way, but here it is:
VB Code:
AppDomain.CurrentDomain.BaseDirectory
-
Aug 11th, 2003, 09:34 AM
#5
Frenzied Member
Application.StartupPath() also seems to do the job.
Don't anthropomorphize computers -- they hate it
-
Aug 25th, 2003, 05:14 PM
#6
Originally posted by vbgladiator
Application.StartupPath() also seems to do the job.
just wanted to say that is what I also use all the time (since it's the simplest of all). I don't know if it makes a big difference, but all these methods seem to work fine. so why not pick application.startuppath? it's just the most straightforward of all
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Sep 14th, 2003, 04:31 PM
#7
PowerPoster
Agreed. It seems like people make this harder than what it is. Application.StartupPath works perfectly for me also.
-
Feb 21st, 2004, 04:38 AM
#8
New Member
VB Code:
dim AppPath as string
AppPath = Environment.CurrentDirectory()
that also works smoothly.
Thanks to all
-
Feb 21st, 2004, 02:22 PM
#9
Originally posted by Sisyphus
VB Code:
dim AppPath as string
AppPath = Environment.CurrentDirectory()
that also works smoothly.
noooooooooo I dont think it's a good idea to use that. I think that currentDirectory can change while your app is running
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Apr 18th, 2004, 06:27 AM
#10
Hyperactive Member
Hoho, try this.
And if you are going to pen a file in your apppath.
VB Code:
Dim file As New System.IO.StreamReader("test.txt")
TextBox1.Text = file.ReadToEnd()
file.Close()
-
Apr 18th, 2004, 02:29 PM
#11
Originally posted by Libero
Hoho, try this.
And if you are going to pen a file in your apppath.
VB Code:
Dim file As New System.IO.StreamReader("test.txt")
TextBox1.Text = file.ReadToEnd()
file.Close()
your second method won't always work if I'm not mistaken (because current directory can change )
plus CurDir is a VB compatibility thing, better not to use it
rate my posts if they help ya!
Extract thumbnail without reading the whole image file: (C# - VB)
Apply texture to bitmaps: (C# - VB)
Extended console library: (VB)
Save JPEG with a certain quality (image compression): (C# - VB )
VB.NET to C# conversion tips!!
-
Aug 29th, 2004, 10:28 PM
#12
New Member
Also try creating a shortcut to your exe and set the start in property to some other path.
Use application.startuppath.
-
Feb 14th, 2005, 10:31 PM
#13
Hyperactive Member
Re: VB.NET - Correct method for app.path
Hi,
I dunno about vb.net but in vb6 app.exename can change in windows xp if you start the program using the shell with the short directory name.
I use GetSetting(app.exename,..)
it changes when i use the short pathname to launch my program.
It is the mark of an instructed mind to rest satisfied with the degree of precision which the nature of the subject admits, and not to seek exactness when only an approximation of the truth is possible.
-Aristotle As quoted in Rapid Development, chapter 8, page 167.
-
Apr 30th, 2007, 12:07 PM
#14
New Member
Re: VB.NET - Correct method for app.path
This C# function can be useful in a library for implementation of a MVC pattern or anything else.
Traduction to VB is simple.
Assert line is optional.
Code:
/// <summary>
/// Get the current path of the executing assembly.
/// ex : If this function currently executed in this library "C:\MyProgram\MyLibrary.dll",
/// this function will return "C:\MyProgram\"
/// </summary>
/// <returns></returns>
private static string GetExecutingAssemblyDirectory()
{
string currentAssembly = Assembly.GetExecutingAssembly().Location; // Get current assembly with filename
int nbCharToKeep = currentAssembly.Length; // Initialize length
// Decrease length until an escape caracter is found
while (currentAssembly[nbCharToKeep - 1] != '\\')
{
nbCharToKeep--;
System.Diagnostics.Debug.Assert(nbCharToKeep >= 0, "GetExecutingAssemblyDirectory() - nbCharToKeep < 0");
}
// Return the dir including the last escape
return currentAssembly.Substring(0, nbCharToKeep);
}
-
Apr 30th, 2007, 02:52 PM
#15
Re: VB.NET - Correct method for app.path
this whole thread can be replaced with one line:
Application.StartupPath
-
Apr 30th, 2007, 03:08 PM
#16
New Member
Re: VB.NET - Correct method for app.path
Microsoft says :
Application.StartupPath Property
Gets the path for the executable file that started the application, not including the executable name.
So, this only works if your library is in the same application path. To avoid coupling a library with a particular application file, I do not recommand to use it in a dll.
-
Jan 10th, 2009, 05:13 AM
#17
New Member
Re: VB.NET - Correct method for app.path
Code:
My.Application.Info.DirectoryPath
works for me in VB 2008
-
Feb 21st, 2010, 04:40 AM
#18
Lively Member
Re: VB.NET - Correct method for app.path
Originally Posted by vbgladiator
Application.StartupPath() also seems to do the job.
This doesn't work if the application is an ActiveX server being called by a client such as Excel. Then it returns the path to the client application instead.
-
Feb 21st, 2010, 07:53 AM
#19
Re: VB.NET - Correct method for app.path
Originally Posted by Kallog
This doesn't work if the application is an ActiveX server being called by a client such as Excel. Then it returns the path to the client application instead.
An ActiveX DLL is just a regular .Net DLL that has an Interface that wraps it for COM objects. To get the DLL's path it looks like you can use: AppDomain.CurrentDomain.BaseDirectory, but if you need the word/excel/whatever path, I think you'll have to have a method or property that the COM object can set, your DLL & .Net wont know where it is unless it's told (passed in manually) where it is.
Last edited by JuggaloBrotha; Feb 25th, 2010 at 02:38 PM.
-
Feb 21st, 2010, 05:31 PM
#20
Lively Member
Re: VB.NET - Correct method for app.path
Originally Posted by JuggaloBrotha
Then it's not a .Net app and thus has nothing to do with this vb.net codebank
Huh? I did that with Visual Basic 2008 Express which can create OLE Automation servers. If it wasn't a .Net app I wouldn't be able to call the .Net function Application.StartupPath().
Anyway I found the other common reply:
Code:
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location())
to correctly give the application exe's directory even when called through COM/ActiveX/OLE automation/whatever you call it.
-
Feb 24th, 2010, 09:54 PM
#21
Lively Member
Re: VB.NET - Correct method for app.path
Originally Posted by JuggaloBrotha
Then it's not a .Net app and thus has nothing to do with this vb.net codebank
Feel free to apologize for your offensively worded mistake.
-
Mar 13th, 2010, 08:47 AM
#22
Junior Member
Re: VB.NET - Correct method for app.path
I'm a relative Visual Studio/VB .net newbie, though experienced in other languages and technologies. This is an interesting thread, not least because of all the effort put in so far that still doesn't answer my particular question.
I am writing a VB .NET web application in visual studio 2008. I want to execute an external program to perform a function for the user, so I need to put the executable somewhere that is independent of its installation machine and accessible by my code. It would seem to make sense to put it in the same place as the EXE and DLL files containing the web application, but I don't know how to access it after that.
Many of the answers given in this thread and other places so far don't seem to fit my environment. If I type "My." into my Visual Studio editor, for instance, it gives me Computer and User, but not Application, as options, so I gather I cannot use My.Application for anything.
Likewise, if I type "Application.s" into the editor, it gives me "Set" and "StaticObjects" as options, but no "Startup" or "Startup()".
Environment.CurrentDirectory gives C:\Windows\system32 at runtime in my development environment; that's not it.
System.Reflection.Assembly.GetExecutingAssembly.Location() gives C:\Users\username\AppData\Local\Temp\Temporary ASP.NET Files\root\fe508d91\60a8c65a\assembly\dl3\b72b7ebd\60f25a6c_b1c2ca01\WebApplication2.DLL at runtime, so that's not it either.
AppDomain.CurrentDomain.BaseDirectory gives C:\Users\username\Documents\Visual Studio 2008\Projects\WebApplication2\WebApplication2\ at runtime in my development environment, and that seems to hold the most promise. Is this one right? If I use that, can I (somehow) package my little executable with my web application and run it from that directory? And if the answer to that is yes, is there some magic to putting that executable in that directory?
It is extraordinary that it takes this much effort to find this out. Why does my Big Fat Visual Basic 2008 book explicitly say that I can use My.Application, but Intellisense in Visual Studio 2008 doesn't recognize it?
-
Mar 13th, 2010, 11:18 PM
#23
Lively Member
Re: VB.NET - Correct method for app.path
Originally Posted by rcook
System.Reflection.Assembly.GetExecutingAssembly.Location() gives C:\Users\username\AppData\Local\Temp\Temporary ASP.NET Files\root\fe508d91\60a8c65a\assembly\dl3\b72b7ebd\60f25a6c_b1c2ca01\WebApplication2.DLL at runtime, so that's not it either.
It looks like that dll is your program yea? I don't have a clue about web applications, but however you got WebApplication2.dll into that folder, can't you do the same thing to put the .exe there too?
Originally Posted by rcook
It is extraordinary that it takes this much effort to find this out. Why does my Big Fat Visual Basic 2008 book explicitly say that I can use My.Application, but Intellisense in Visual Studio 2008 doesn't recognize it?
It may need a reference to be added. Type in the my.application call and use the mouseover dropdown box of 'error correction options'.
-
Mar 14th, 2010, 07:22 AM
#24
Re: VB.NET - Correct method for app.path
Originally Posted by rcook
I'm a relative Visual Studio/VB .net newbie, though experienced in other languages and technologies. This is an interesting thread, not least because of all the effort put in so far that still doesn't answer my particular question.
I am writing a VB .NET web application in visual studio 2008. I want to execute an external program to perform a function for the user, so I need to put the executable somewhere that is independent of its installation machine and accessible by my code. It would seem to make sense to put it in the same place as the EXE and DLL files containing the web application, but I don't know how to access it after that.
Many of the answers given in this thread and other places so far don't seem to fit my environment. If I type "My." into my Visual Studio editor, for instance, it gives me Computer and User, but not Application, as options, so I gather I cannot use My.Application for anything.
Likewise, if I type "Application.s" into the editor, it gives me "Set" and "StaticObjects" as options, but no "Startup" or "Startup()".
Environment.CurrentDirectory gives C:\Windows\system32 at runtime in my development environment; that's not it.
System.Reflection.Assembly.GetExecutingAssembly.Location() gives C:\Users\username\AppData\Local\Temp\Temporary ASP.NET Files\root\fe508d91\60a8c65a\assembly\dl3\b72b7ebd\60f25a6c_b1c2ca01\WebApplication2.DLL at runtime, so that's not it either.
AppDomain.CurrentDomain.BaseDirectory gives C:\Users\username\Documents\Visual Studio 2008\Projects\WebApplication2\WebApplication2\ at runtime in my development environment, and that seems to hold the most promise. Is this one right? If I use that, can I (somehow) package my little executable with my web application and run it from that directory? And if the answer to that is yes, is there some magic to putting that executable in that directory?
It is extraordinary that it takes this much effort to find this out. Why does my Big Fat Visual Basic 2008 book explicitly say that I can use My.Application, but Intellisense in Visual Studio 2008 doesn't recognize it?
WebApp's don't have access to the local system like WinForm's apps do. This is why you don't have an Application.StartupPath, because if you did, it's the location on the web server, not the person's local machine who's viewing your site in their web browser. When you're developing the webapp AppDomain.CurrentDomain.BaseDirectory returns the path to the project on your local computer because your computer is the web server, all be it a fake one, but one nonetheless. Once you upload the site to the web server, you're going to get the root path of where it is on the file system of the server.
What you can do is make an old school activex control that the user can download from your site (they will be prompted before hand, there's no way around that) that can interact with the local computer, it's how Microsoft does it for WinXP's Windows/Microsoft Updates. How you would go about doing this is beyond me as I've never made an activex browser control before.
-
May 24th, 2010, 09:29 AM
#25
New Member
Re: VB.NET - Correct method for app.path
Here's a toughy for ya. my executable is in a folder on a network drive. My R: drive is mapped to a folder a few levels above that folder. when i use ANY of these methods I get this:
"R:\whatever\whateversub\myfolder\my.exe"
thats not what i want. i want the actual path like this:
"\\first\second\third\whatever\whateversub\myfolder\my.exe"
can anyone figure this one out for me?
please and thanks!
-JeZteR
-
May 24th, 2010, 10:25 AM
#26
Re: VB.NET - Correct method for app.path
When you map a network drive you get a path that has a local drive letter in your "My Computer" and when you use anything through that then that's the path you have, which is also the path your program has too. What you can do is make a network shortcut to "\\first\second\third\whatever\whateversub\myfolder\my.exe" in your "Network Neighborhood" or the desktop (which works like a folder/file shortcut except it has the \\first part included in it) and the program will have/know the same path too.
-
May 24th, 2010, 05:19 PM
#27
New Member
Re: VB.NET - Correct method for app.path
so theres no GetFullPath or GetActualPath that i could pass a mapped drive or folder to? that sux lol
ok thanks for the reply
-JeZteR
-
May 24th, 2010, 10:19 PM
#28
Lively Member
Re: VB.NET - Correct method for app.path
Originally Posted by jeztericp
so theres no GetFullPath or GetActualPath that i could pass a mapped drive or folder to? that sux lol
ok thanks for the reply
-JeZteR
A network path isn't really more "full" or "actual" than a local one. It's just different. Here's a code that I think finds the network location of a local drive letter.
http://www.mvps.org/access/api/api0003.htm
So you'd find the local path, then convert the drive letter to the network path.
-
May 25th, 2010, 07:10 AM
#29
Re: VB.NET - Correct method for app.path
Here's a much simpler example that I wrote that gets the UNC path that a network drive maps to - it uses the same API as that example in the previous post but I think you will agree it is much more readable and .NET friendly http://cjwdev.wordpress.com/2009/12/...network-drive/
-
May 25th, 2010, 08:20 AM
#30
Lively Member
Re: VB.NET - Correct method for app.path
Bloody jesus it is :P But I wonder why there isn't a .net function for that.
-
May 25th, 2010, 08:49 AM
#31
Re: VB.NET - Correct method for app.path
Originally Posted by chris128
Here's a much simpler example that I wrote that gets the UNC path that a network drive maps to - it uses the same API as that example in the previous post but I think you will agree it is much more readable and .NET friendly http://cjwdev.wordpress.com/2009/12/...network-drive/
Now that, is neat. I've wondered from time to time how windows knows a mapped drive vs a local drive, I've just never looked into it.
-
May 25th, 2010, 09:54 AM
#32
Re: VB.NET - Correct method for app.path
haha thanks hope its useful to someone
-
May 26th, 2010, 07:42 PM
#33
New Member
Re: VB.NET - Correct method for app.path
I was really hoping I wouldn't have to use the old stuff(api). I really was hoping there was a .net way to do it. I have found that they have "forgotten" to include several things in .net that you could do in vb6.
This will definitely work for now. Thank you all very much.
-JeZteR
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
|