application path

B

Berryl Hesh

I want to access a development db at a known location (ie,
"MyApp\MyData\MyDb.mdb") but an unknown machine. I've used
"|DataDirectory|\MyDb.mdb" in app.config with some limited success before
but it's not working out well here.

As a string with substitution variables, the connection (to a legacy MS
Access db w/ security) is basically:
string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data
Source={1}MyDb.mdb;Jet OLEDB:System Database={1}Security.mdw;User
ID={2};Password={3};",
PROVIDER_JET,
directoryPath,
userID,
password);

I thought I might be able to get what I wanted by IO and reflection easier,
but am stuck. The code below looks like what I want but when I run it from
test code (in a separate assembly) it gives me the path to debug in the Test
assembly (ie "MyApp\Tests\MyData\bin\Debug") instead of the App (ie
"MyApp\MyData\bin\Debug") assembly.
public static string CurrentDevelopmentDirectory() {
var assembly = Assembly.GetAssembly(typeof (Connection)).CodeBase;
return Path.GetDirectoryName(assembly);
}

Thanks for the help! BH
 
C

Ciaran O''Donnell

You should call Assembly.GetExecutingAssembly() to get the actually exe that
is running. Then you can get the CodeBase property of that to get the path
 
B

Berryl Hesh

Hi Ciaran & thanks for the reply, but that also gets me to
MyApp\*Tests*\bin\Debug.

According to the docs, it makes sense that GetExecutingAssembly() would do
that when the executing assembly is in fact Tests. The frustrating part is
that it makes sense that if MyCoreClass is located in MyApp\Core, and I call
Assembly.GetAssembly(typeof (MyCoreClass)).CodeBase, I should have
MyApp\*Core*\bin\Debug even when the executing assembly is from Tests. This
is the problem I'm trying to solve.
 

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