2013-04-13 37 views
0

我想从特定路径位置读取文件。该位置在本地服务器和在线服务器上具有不同的磁盘盘符和目录结构。。模式

所以我想,缺省设置文件路径,以满足我的在线服务器磁盘,当我调试应用程序与本地文件路径来工作。

我试着用这个

private const string _configurationFilePath = @"E:\web\mysite\"; 
#if DEBUG 
enter code here 
_configurationFilePath = @"D:\mywebprj\mysite\"; 
#endif 

我收到错误消息已经包含了_configurationPath

定义有没有更好的方式来此不使用配置文件和其他手工输入的解决方案?

感谢

回答

1

使用#if DEBUG #else #endif

#if DEBUG 
    private const string a = @"d:\"; 
#else 
    private const string a = @"e:\"; 
#endif 
2

您使用private const string所以它不能被修改。写你这样的代码:

#if !DEBUG 
    private const string _configurationFilePath = @"E:\web\mysite\"; 
#endif 
#if DEBUG 
enter code here 
    private const string _configurationFilePath = @"D:\mywebprj\mysite\"; 
#endif 

BTW更好的主意是,而不是存储harcoding它的配置这条道路。

2

一个更好的解决方案。您可以在项目文件的构建部分中使用复制msbuild任务。

您可以根据使用的术语应用解决方案配置名称创建多个app.config中。$(配置)的.config。例如:app.Debug.config。 app.Release.config等

然后使用的MSBuild复制任务只有一个应用程序配置文件作为MyProject.exe.config复制到$(OUTDIR)。

+1

+1命令使用配置文件。 –