none
c#程式 RRS feed

  • 問題

  • 請問各位高手  我現在有第一個程式可讀取檔案並找到Z並增加Z後面的字串程式 並將改好的黨在直接存檔  此改好的擋會顯示在label上

    但我現在更改了找Z的方法以及Z字串後面更改的方法 如第二個程式

    那我因該如何將第二個檔 放在第一個程式內使用 請各位高手指導一下 

    第一個程式

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.IO;

    using System.Text.RegularExpressions;

     

     

    namespace WindowsFormsApplication1

    {

        public partial class Form1 : Form

        {

            public Form1()

            {

                InitializeComponent();

            }

          

            string savefile;//存檔用

            StreamWriter sw;//存檔用

            string line;//讀取檔案

         

     

            private void button1_Click(object sender, EventArgs e)

            {

                {

                    //建立讀檔功能

                    Stream myStream = null;

                    OpenFileDialog openFileDialog1 = new OpenFileDialog();

                    openFileDialog1.InitialDirectory = "c:\\";

                    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

                    openFileDialog1.FilterIndex = 2;

                    openFileDialog1.RestoreDirectory = true;

                   

                    //確認是否成功開啟讀檔功能

                    if (openFileDialog1.ShowDialog() == DialogResult.OK)

                    {

                        try

                        {

                            if ((myStream = openFileDialog1.OpenFile()) != null)//確認讀檔功能是否正常

                            {

                                using (myStream)

                                {

                                    string path = openFileDialog1.FileName;//記錄讀檔名稱

                                    StreamReader sr = new StreamReader(path);//讀檔功能

                                    savefile = @Application.StartupPath + "\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".txt";//設定存檔位置

                                    sw = new StreamWriter(savefile, true);//設定存檔於同一位置

     

                                    while (!sr.EndOfStream)//當讀取檔案的文件沒問題時進行後續分析

                                    {

                                        line = sr.ReadLine();//開始進行每行讀取

     

                                        if (line.Contains("Z"))//確認是否有大寫Z存在

                                        {

                                            string www = Convert.ToString(line.IndexOf("Z"));//Z的位置

                                            string strTarget = line.Insert(line.IndexOf("Z") + 1, "GOOD");//Z的後面加入GOOD

                                            label1.Text += strTarget + "\n";//顯示結果在LABEL1

                                            sw.WriteLine(strTarget);//將修改後的結果寫入檔案

                                        }

                                        else

                                        {

                                            label1.Text += line + "\n";//將不含Z的程式顯示在LABEL1

                                            sw.WriteLine(line);//將不含Z的程式寫入檔案

                                        }

                                       

                                    }

                                    sw.Close();//關閉寫入檔案

                                }

                            }

                        }

                        catch (Exception ex)//讀取時找不到檔案顯示如下

                        {

                            MessageBox.Show("找不到正確檔案" + ex.Message);

                        }

                    }

     

                }

            }

     

            private void label1_Click(object sender, EventArgs e)

            {

     

            }

        }

    }

    第二個程式 此程式是先建立在 主控台應用程式 (完整)

    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)

            {

               List<string> testLists = new List<string>() {

         "X10z10"     

         ,"Z1.0S2B11EWQ"

         ,"z1AVVSS"

         ,"z1.GRWW QD"

         ,"Z10F1000"

         };

         Console.WriteLine("*** Before ***");

         foreach (string s in testLists)

         {

             Console.WriteLine(s);

         }

         Console.WriteLine("*** After ***");

         foreach (string s in testLists)

         {

             Console.WriteLine(ZMultiply(s));

           

         }

         Console.ReadKey();

    }

     

     

            static string ZMatchReplace(Match m)

            {

                string result = m.ToString();

                string zSubPattern = @"[\d+]+[\d+(\.\d+)]+";

                Regex rx = new Regex(zSubPattern);

                result = rx.Replace(result, new MatchEvaluator(ZMatchReplaceDigi));

                return result ;

            }

     

        static string ZMatchReplaceDigi(Match m)

        {

            string result = m.ToString();

            decimal digiValue =0 ;

            decimal.TryParse(result, out digiValue);

            //要乘的數值

           

            const int x = 2;

            return (digiValue * x).ToString() + "B"+"-"+(digiValue * x).ToString();

        }

    static string ZMultiply(string orgValue)

    {

        string z樣式 = @"[z][\d+]+[\d+(\.\d+)]+";

        Regex rx = new Regex(z樣式, RegexOptions.IgnoreCase);

        string result = rx.Replace(orgValue, new MatchEvaluator(ZMatchReplace));

        return result;

    }

            }

        }

    2014年11月24日 上午 04:15

解答

所有回覆