Application path

  • Thread starter Thread starter ORC
  • Start date Start date
O

ORC

Hi,

I have an application in which I need information of the path to the exe
file. The application must run on both compact and full framework and on the
Internet I found this code snip that should work (on the CF at least):
"Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)"

It works very fine on Compact Framework where I get:
"\Program Files\My Company"

But on the full framework a "file:" and a backslash is applied in front of
the resulting path as in:
"file:\C:\Program files\My Company"

What is wrong and how is the correct way to do it( that will work on both CF
and FF ?

Thanks,
Ole
 
Hi Ole,

I haven't got any practise with CompactFramework.
But if you want to get path of executing binary then you can use:
Application.ExecutablePath
or
Application.StartupPath - to get its directory

HTH
Marcin
 
Also for a Full Framework you must use
System.Reflection.Assembly.GetEntryAssembly().Location. At runtime you
can detect framework version (System.Environment.Version) and switch
between them.

Best regards,
Sergey Bogdanov
 
It is not possible to check OS version during runtime and then use the
appropiate path detect code line because the compiler doesn't have a
definition for GetEntryAssembly in CF. Some other idea?

Thanks,
Ole
 
The CF doesn't contain a definition for ExecuteablePath or StartupPath so
the program wont compile.

Thanks
Ole
 
Sorry, my mistake. Try to use P/Invoke GetModuleFileName (coredll.dll)
and GetModuleFileName (kernel32.dll).

Best regards,
Sergey Bogdanov
 
On the Compact Framework you could use this alternative to
GetEntryAssembly:-
http://blog.opennetcf.org/pfoot/PermaLink.aspx?guid=664c2f88-cadb-4db1-99ed-3dc8e7525cc4

If you merely want the same result from
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase on
both frameworks then use:-

string path =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);
if(Environment.OsVersion.Platform!=PlatformID.WinCE)
{
path = path.Replace("file:\\","");
}

This will remove the prefix only on the desktop OS. Although you could call
this replace on the device also since the path won't contain this string so
no change will take place.

Peter
 
.... even better solution :)

string codeBase = Assembly.GetExecutingAssembly().GetName().CodeBase;
if (codeBase.StartsWith(@"file:\")) codeBase = codeBase.Substring(6);


Best regards,
Sergey Bogdanov
 
Oh yes it does, but GetEntryAssembly is not defined in CF and therefore the
app wont compile.

Thanks
Ole
 
Thanks All,

The solution with getting rid of the leading "file:\" seems to be the one
I'm looking for!

Thanks,
Ole
 
Also using GetCallingAssembly may be better than GetExecutingAssembly. If
your code is in a library that is GAC installed, you'll get a path to the
GAC folder, not the app folder with GetExecutingAssembly

--
<ctacke/>
www.OpenNETCF.org
Your CF searches start and end here
 
For the sake of integrity I need to point out that the correct way of
"getting rid of" file:// is to use the Uri class and its accessors
 
Back
Top