2012-02-26 135 views
0

我的绝对路径,为我工作:如何更改为C#Windows中的相对路径的绝对路径表单

const string fileName = 
@"C:\Udvikling\MapEditor\MapEditorProject\Data\Track1.xml"; 

但我希望有一个相对路径。 我try'ed:

const string fileName = @"~\Data\Track1.xml"; 

我得到这个错误:

“找不到路径 一部分C:\ Udvikling \ RollerApp \ RollerGame \ MapEditorProject \ BIN \调试\〜\数据\ Track1.xml'“

回答

1

不要添加~字符并使用Path.Combine方法来组合2个路径。

或多或少是这样的:

const string fileName = @"\Data\Track1.xml"; 

string combinedPath = Path.Combine(@"C:\Udvikling\RollerApp\RollerGame\MapEditorProject\bin\Debug", 
     fileName); 

希望这有助于。

+0

啊是的,这是正确的。谢谢。但是,是不是有一个Application.SomethingStatic我应该用于轨道的“左”部分? – radbyx 2012-02-26 21:32:15

+0

@radbyx:您可能引用[Application.StartupPath](http://msdn.microsoft.com/zh-cn/library/system.windows.forms.application.startuppath.aspx) – Tigran 2012-02-26 21:34:45

+0

是的!它可以取代我的第一部分。但它并不是全部。但也许这只是我。也许我在做其他事情也是错的。 XML文件应该位于bin目录中的某个位置。因为路径的第一部分永远不会在另一台计算机上正确运行? – radbyx 2012-02-26 21:38:48