2016-01-04 75 views
-3

我需要在VS2010中使用正則表達式的幫助。我想找到:VS2010查找並替換數字列表

Environment.Exit(0); ... 
Environment.Exit(0); 

,取而代之的是:

File.WriteAllText("code",1); Environment.Exit(0); ... 
File.WriteAllText("code",2); Environment.Exit(0); 

我只需要知道我的程序終止。所以我有很多Environment.exit在不同的文件。

+2

一個更好的選擇將是您的通話重構爲'Environment.Exit(..)'。這會給你一個從編碼角度退出的單點,你可以在離開應用程序之前檢查調用堆棧。 –

+0

我有50多個Env.Exit()的調用只是technickly我需要更換此代碼 –

回答

0

試試這個

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Text.RegularExpressions; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      string input = "Environment.Exit(0);" + 
          "Environment.Exit(0);"; 
      string output = Regex.Replace(input, @"Environment.Exit\(0\);", new MatchEvaluator(MyReplacement)); 
     } 
     static int count = 1; 
     static string MyReplacement(Match match) 
     { 
      return string.Format("File.WriteAllText(\"code\",{0}); Environment.Exit(0);", count++); 
     } 
    } 
}​ 
+0

謝謝,但我仍然希望標準的視覺工作室搜索工具將解決我的問題。但是,正如你所建議的,解決這一任務的最有效的方法是編寫另一個程序來完成主程序的編寫。 –

+0

VS編輯器沒有像一些更復雜的編輯器那樣增加數值的宏功能。 – jdweng