2016-11-15 8 views
0

私がフォローを持っているC //接頭辞:削除ファイル:#

string file = string.Concat(
       System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase 
       ), 
       "bla.xml"); 

今、ファイルは次のとおりです。

file:\C:\test\Debugbla.xml 

どのように私は、 "ファイル:\" を削除することができプレフィックスを?

おかげ

+4

'.Lplace(" file:\ "、" ") – sed

答えて

6

UriクラスはUriを操作する方法です。 string.Replaceはオプションですが、必要に応じてエスケープルールを処理する特別なケースのために明示的に設計されたツールを使用することをお勧めします。

string file = string.Concat(System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bla.xml"); 

file = new Uri(file).AbsolutePath; 
+0

[LocalPath](https://msdn.microsoft.com/en-us/library/system.uri.localpath%28v=vs.110% 29.aspx)はおそらくもっと良くフィットしますが、実際にUriクラスはUrisを扱う正しい方法です。 –

0
file = file.Replace(@"file:\", ""); 

あなたは空の文字列に置き換えることができます。

0

パスの\ partのようにファイルを置き換えることができます。

string file = string.Concat(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase), "bla.xml"); 
file = file.Replace(@"file:\",""); 
1

通常のファイルパスを返す代わりに、Assembly.Locationプロパティを使用。

また、Path.Join()を使用してください。