[dba-VB] C# get project directory

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Mon Sep 21 09:28:46 CDT 2009


Hi John,

Will that work for you?:

string appFullPath = Application.ExecutablePath;
string appFolder = System.IO.Path.GetDirectoryName(appFullPath);
System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(appFolder);
Console.WriteLine(folder.Parent.Parent.Parent.FullName);

BTW, if you're planning to have your (sample) application easily deployed
using XCOPY then it's better to not use the above approach as it assumes
that your executable will have "some useful stuff" three levels up in the
directory tree - you'd better use assumption that your app has "some useful
stuff" in predefined (sub-)...sub-directories relative to the executable's
directory.

I usually create a \Bin folder under solution's folder - that will be 

E:\C# Projects\Grade Calculator\Bin

for your case and I set both Debug and Release mode executables built in
this folder (Solutions
Explorer->Project->Right-Click->Properties->Build->Output Path). Then all
"useful stuff" can be placed in custom \Bin folder (E:\C# Projects\Grade
Calculator\Bin) and its subfolders, and XCOPY deployment will always work
OK.

I'd also note that you use Application object to get exe fullpath - that
works by default for WinForms apps but is unavailable in console and other
types of apps by default: yes, you can set a reference to
System.Windows.Forms in console application but that would be an overkill -
I usually use this function:

public static string ExecutingAssemblyFullPath
{
    get
    {
        Assembly asm = Assembly.GetExecutingAssembly();
        AssemblyName origCodebase = asm.GetName(false);
        string codeBase = origCodebase.CodeBase;
        if (codeBase.StartsWith(@"file:///") ||
codeBase.StartsWith(@"file:\\\"))
            codeBase = codeBase.Substring(8);
        return codeBase;
    }
}

I have got made it quite some time ago, and it always work well, although
there could be even better/shorter solution now...

Thank you.

--
Shamil 


-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of jwcolby
Sent: Monday, September 21, 2009 5:42 PM
To: VBA
Subject: [dba-VB] C# get project directory

I want to set up a directory to store files directly under the project
directory.  I can get the exe 
directory using Application.ExecutablePath but that is clear down in the bin
directory etc.

I want THIS path:

E:\C# Projects\Grade Calculator	<<<<<

I have Googled till I am blue in the face and all I end up with is (where
the exe is):

D:\C# Project\Grade Calculator\Grade Calculator\bin\Debug

Any help appreciated.

Thanks,

-- 
John W. Colby
www.ColbyConsulting.com
_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com


__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4442 (20090921) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru


 

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 4443 (20090921) __________

The message was checked by ESET NOD32 Antivirus.

http://www.esetnod32.ru
 




More information about the dba-VB mailing list