tags:

views:

24

answers:

2

I have some xml files, which are used in my application. They are stored in the same folder with application , in subfolder DATA: "C:\MyProject\DATA\". To get the DATA folder path i use this code :

 static public string GetDataFolderPath()
 {
      string s = System.IO.Directory.GetCurrentDirectory().Replace(@"\bin\Debug", ""); 
      int i = s.LastIndexOf(@"\");
      s = s.Substring(0, i);
      i = s.LastIndexOf(@"\");
      s= s.Substring(0, i);
      return s + @"\Data\"; 
 }

So when i want to deploy my application, i create a setup project, and add the DATA folder to Application folder. But after i install the program f.e. "C:\Project"(DATA folder- "C:\Project\DATA" i got the error: "folder C:\DATA is not found". What i need to change to make things working after deployment. Why it looks for the DATA folder on 1 level higher?

A: 

Maybe current directory (during launching your program) is not the same one that assemblies lie?

try:

//get the full location of the assembly 
string fullPath = System.Reflection.Assembly.GetAssembly(typeof(<your class name>)).Location;

//get the folder that's in
string theDirectory = Path.GetDirectoryName( fullPath );

or

string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
razor
A: 

Try this, it might work better:

public static string GetDataFolderPath()
{
#if DEBUG
    // This will be executed in Debug build
    string path = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", "");
#else
    // This will be executed in Release build
    string path = Directory.GetCurrentDirectory();
#endif
    return Path.Combine(path, "Data");
}

Or just this if you want one for both Debug and Release builds:

public static string GetDataFolderPath()
{
    string path = Directory.GetCurrentDirectory().Replace(@"\bin\Debug", "");
    return Path.Combine(path, "Data");
}

You have to add using System.IO; for this to work.

Jens Granlund
thanks a lot, that is what i was looking for
Mike