2010-05-20 5 views
0

내 응용 프로그램에 사용되는 일부 XML 파일이 있습니다. 이들은 하위 폴더 DATA : "C : \ MyProject \ DATA \"에 응용 프로그램이있는 동일한 폴더에 저장됩니다. 내가이 코드를 사용하는 데이터 폴더 경로를 얻으려면 : 난 내 응용 프로그램을 배포 할 때설정 마법사를 사용하여 Windows 응용 프로그램을 배포 할 때의 문제

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\"; 
} 

그래서, 내가 설치 프로젝트를 만들고, 그리고 응용 프로그램 폴더에 데이터 폴더를 추가 할 수 있습니다. 그러나 프로그램을 설치 한 후 f.e. "C : \ Project"(데이터 폴더 - "C : \ Project \ DATA"오류가 발생했습니다 : "C : \ DATA 폴더를 찾을 수 없습니다" 배포 후 작업을 수행하기 위해 변경해야하는 이유 ? 1 개 수준 높은

답변

0
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"); 
} 

아니면 디버그 및 릴리스 모두를 원한다면 바로이 빌드를 :, 그것은 더 잘 작동 할 수이 시도

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

당신이 작동하지 using System.IO;을 추가해야합니다.

+0

감사합니다. 그게 내가 찾고 있던 것입니다. – Mike

0

어쩌면 현재 디렉토리에 데이터 폴더에 대한 어셈블리 거짓말 같은 일이 아닌 (프로그램을 실행하는 동안)

시도 :

//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); 

또는

string codeBase = Assembly.GetExecutingAssembly().CodeBase; 
UriBuilder uri = new UriBuilder(codeBase); 
string path = Uri.UnescapeDataString(uri.Path); 
return Path.GetDirectoryName(path); 
관련 문제