Application path

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
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

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
 
S

Sergey Bogdanov

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
 
O

ORC

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
 
O

ORC

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

Thanks
Ole
 
S

Sergey Bogdanov

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

Best regards,
Sergey Bogdanov
 
P

Peter Foot [MVP]

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
 
S

Sergey Bogdanov

.... even better solution :)

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


Best regards,
Sergey Bogdanov
 
O

ORC

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

Thanks
Ole
 
O

ORC

Thanks All,

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

Thanks,
Ole
 
C

Chris Tacke, eMVP

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
 
A

Alex Feinman [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top