Finding App's path from NetCF

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a way from within NetCF to find out the path on the device that the
Exe is running from?

Thanks.
 
One note is that I consider this a bad implementation. Instead of
GetExecutingAssembly() it should be using GetCallingAssembly(). Where this
comes into play is if you put it in a DLL, and then put it in the GAC,
you'll get back '\Windows' as your path, which is probably not what you
intended.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
I tried GetCallingAssembly in my program and the path returned as \Windows.
GetExecutingAssembly returned the full path and name of the executing
program.

James
 
That makes no sense to me. Per the docs, GetCallingAssembly returns the
assembly of the method that invoked the currently executing method. Unless
this was a callback or maybe an eventhandler from a GAC assembly I can't see
how that would occur.

-Chris
 
The most fool-proof method is actually to resort to a P/Invoke. Take a look
at the code for OpenNETCF.Reflection.AssemblyEx from the SDF:-
http://vault.netcf.tv/VaultService/.../OpenNETCF/Reflection/AssemblyEx.cs&version=2
This uses the GetModuleFileName API to recreate the
Assembly.GetEntryAssembly() method available on the desktop. This will
always return the correct name of the executable regardless of whether
called in the exe itself, a dll or a dll in the GAC

Peter
 
I agree it is not per the docs, but I have run into several situations that
do not work according to the docs. I think the docs need some work.

James Lysaght
 
In this case I wouldn't think it's a doc problem. If you're getting
\Windows for GetCallingAssembly it sounds more like a bug.

--
Chris Tacke
Co-founder
OpenNETCF.org
Has OpenNETCF helped you? Consider donating to support us!
http://www.opennetcf.org/donate
 
And here is the correct working code.

Public Declare Function GetModuleFileName Lib "Coredll" _
Alias "GetModuleFileNameW" _
(ByVal hModule As Int32, _
ByVal lpFileName As String, _
ByVal nSize As Int32) As Int32

Public Function App_Path() As String
Dim bufferV As New String(CChar(" "), 301)
Dim retVal As Long
Dim iRetVal As Integer
retVal = GetModuleFileName(0&, bufferV, 300)
bufferV = Strings.Left(bufferV, retVal)
iRetVal = Strings.InStrRev(bufferV, "\")
If iRetVal > 0 Then
bufferV = Strings.Left(bufferV, iRetVal)
Return bufferV
Else
Return Nothing
End If
End Function
 
You are aware of the System.IO.Path class, right?

string appPath = Path.GetDirectory(GetModuleFileName());

-Chris


"Alexandre Lafrance-Drouin" <Alexandre
(e-mail address removed)> wrote in message
 
Back
Top