2016-10-24 15 views
-3

を組み合わせる:どのように私は2つの異なるパス持つ2つの異なるパスに

C:\Destination\New\Place\Bin\Debug\output\CustomCompanyNames\file\file.xml

:私は、2つの2本の異なるパスから

期待パスの値を取得する方法を必要とする

C:\Project\v4.0\Tool\Custom\CustomCompanyNames\Template\file\file.xml C:\Destination\New\Place\Bin\Debug\output

をどうすれば解決できますか?

  • カスタムは、税関後のすべてのディレクトリが異なる名前
  • を持って修正フォルダ
  • ある
悪いプログラム

私の解決策:最初のパス

  • 先秒イスト

    • カスタム経路

      private void test() 
          { 
          string result = destination; 
      
          string[] custom = customs.Split('\\'); 
      
          foreach (var s in custom) 
          { 
      
           if(s.Contains("custom") || result.Contains("custom")) 
           { 
            if(s.Contains("templates")) break; 
            result = Path.Combine(result, s); 
           } 
          } 
      } 
      
  • +1

    何か試しましたか?上のパスを分割した後、あなたは 'Path.Combine'を使うことができます –

    +0

    パスを分割してコードを結合しようとしました。しかし、私の問題は、ディレクトリ\テンプレートを整えることです。 – mimu1011

    +0

    誰も私のためにそれを行うとは言いません。私は@GiladGreen – mimu1011

    答えて

    1

    代わりのSplitティンCustom部分と、それからSubstringを見つけるためのパスを使用IndexOf

    string path1 = @"C:\Project\v4.0\Tool\Custom\CustomCompanyNames\Template\file\file.xml"; 
    string path2 = @"C:\Destination\New\Place\Bin\Debug\output"; 
    
    string splitter = @"Custom\"; 
    string desiredSection = path1.Substring(path1.IndexOf(splitter) + splitter.Length); 
    
    string output = Path.Combine(path2, desiredSection); 
    
    +1

    ありがとう。コードは簡単に見えます:)しかし、出力はC:\\デスティネーション\\新しい\\場所\\ビン\\デバッグ\\出力\\カスタムカスタム\テンプレート\\テンプレート\\ファイル\\ file.xmlです。テンプレート\\ ? – mimu1011

    +0

    @Mo_Code - これはフォローアップの質問です...私の答えはまだ2つのパスに参加する方法の元の問題を解決しました。 'Template'パートを削除したい場合、' .Replace() 'を使うことができます。 –

    関連する問題