2017-04-20 40 views
-1

我正在搜索MIS幾個*.INI文件。然後我更改該條目的值並再次保存文件。我現在正在尋找一種方式,程序將顯示不包含MIS條目的INI文件列表。C#返回標籤中不包含字符串的文件列表

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
     pictureBox1.Visible = false; 
     pictureBox2.Visible = false; 
     label1.Visible = false; 
     label3.Visible = false; 
     label4.Visible = false; 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Visible = false; 
     pictureBox2.Visible = false; 
     label1.Visible = false; 
     label3.Visible = false; 
     label4.Visible = false; 

     try 
     { 
      DirectoryInfo d = new DirectoryInfo(@".\"); 
      FileInfo[] Files = d.GetFiles("*.INI"); 

      if (Files.Length == 0) 
      { 
       label3.Visible = true; 
       label3.Text = "No INI files found in the directory!"; 
       label3.ForeColor = Color.Red; 
       label4.Visible = true; 
       label4.Text = "Ensure the program is run from inside the !MineIT folder"; 
       label4.ForeColor = Color.Red; 
      } 
      else 
       foreach (FileInfo file in Files) 
       { 
        string text = File.ReadAllText(file.Name); 
        const string PATTERN = @"MIS (?<Number>[\d\.]+)"; 
        Match match = Regex.Match(text, PATTERN, RegexOptions.RightToLeft); 

        if (match.Success == false) 
        { 
         label3.Visible = true; 
         label3.Text = "No MIS entry found in the INI file!"; 
         label3.ForeColor = Color.Red; 
         label4.Visible = true; 
         label4.Text = "Ensure MIS has been defined in the INI file"; 
         label4.ForeColor = Color.Red; 
        } 
        else 
        { 
         int index = match.Groups["Number"].Index; 
         int lenght = match.Groups["Number"].Length; 

         text = text.Remove(index, lenght); 
         text = text.Insert(index, "1"); 

         label1.Text = ("SERVER ENABLED"); 
         label1.ForeColor = Color.Green; 
         label1.Visible = true; 
         pictureBox1.Visible = true; 
         File.WriteAllText(file.Name, text); 
        } 
       } 
      } 
     catch 
     { 
     } 
    } 

    private void button2_Click(object sender, EventArgs e) 
    { 
     pictureBox1.Visible = false; 
     pictureBox2.Visible = false; 
     label1.Visible = false; 
     label3.Visible = false; 
     label4.Visible = false; 

     try 
     { 
      DirectoryInfo d = new DirectoryInfo(@".\"); 
      FileInfo[] Files = d.GetFiles("*.INI"); 

      if (Files.Length == 0) 
      { 
       label3.Visible = true; 
       label3.Text = "No INI files found in the directory!"; 
       label3.ForeColor = Color.Red; 
       label4.Visible = true; 
       label4.Text = "Ensure the program is run from inside the !MineIT folder"; 
       label4.ForeColor = Color.Red; 
      } 
      else 
      { 
       foreach (FileInfo file in Files) 
       { 
        string text = File.ReadAllText(file.Name); 
        const string PATTERN = @"MIS (?<Number>[\d\.]+)"; 
        Match match = Regex.Match(text, PATTERN, RegexOptions.RightToLeft); 

        if (match.Success == false) 
        { 
         label3.Visible = true; 
         label3.Text = "No MIS entry found in the INI file!"; 
         label3.ForeColor = Color.Red; 
         label4.Visible = true; 
         label4.Text = "Ensure MIS has been defined in the INI file"; 
         label4.ForeColor = Color.Red; 
        } 
        else 
        { 
         int index = match.Groups["Number"].Index; 
         int lenght = match.Groups["Number"].Length; 

         text = text.Remove(index, lenght); 
         text = text.Insert(index, "0"); 

         label1.Text = ("SERVER DISABLED"); 
         label1.ForeColor = Color.Red; 
         label1.Visible = true; 
         pictureBox2.Visible = true; 
         File.WriteAllText(file.Name, text); 
        } 
       } 
      } 
     } 
     catch 
     { 
     } 
    } 

    private void button3_Click(object sender, EventArgs e) 
    { 
     Close(); 
    } 
} 

我對編程非常陌生,我希望有人能夠幫助我解決這個問題。

我已經搜索了2天,並沒有找到太多的幫助。

我想要顯示的結果爲:

label3.Text = "No MIS entry found in the INI files: a, b, c, d, e 

感謝

回答

0

定義字符串列表,將持有的文件的文件名沒有MIS

private void button1_Click() 
{ 
    List<string> filesWithoutMIS = new List<string>(); 
    // rest of your code 

後來,通過正則表達式MIS檢查時,添加文件名列表

if (!match.Success) 
{ 
    filesWithoutMIS.Add(file.Name); 
    label4.Visible = true; 
    label4.Text = "Ensure MIS has been defined in the INI file"; 
    label4.ForeColor = Color.Red; 
} 
你的方法結束

,後foreach循環,寫信息

label3.Text = "No MIS entry found in the INI files: " + string.Join(", ", filesWithoutMIS.ToArray()); 
+0

非常感謝你這有幫助,它正在工作! –

+0

@HendrikSidaway你可以說,感謝標記答案爲解決方案:) – Nino

0

創建List<string>您儲存的文件的名稱,而不需要的文本,在循環後,檢查是否有名稱button1_Click根據該名單,並顯示一切

List<string> noMatch = new List<string>(); 
foreach(FileInfo file in Files) 
{ 
    ... 
    if (match.Success == false) 
     noMatch.Add(file.Name); 
    else 
     .... 
} 

if(noMatch.Count > 0) 
{ 
    label3.Visible = true; 
    label3.Text = $"Files {string.Join(",", noMatch)} have no MIS entry !"; 
    label3.ForeColor = Color.Red; 
    label4.Visible = true; 
    label4.Text = "Ensure the program is run from inside the !MineIT folder"; 
    label4.ForeColor = Color.Red; 
} 
相關問題