讀寫檔案.替換字串

來源:互聯網
上載者:User
代碼

        private void CreateNewFile(string cppName)
        {
            this.textBox2.Clear();
            ////////////////////
            //讀模板檔案
            string strFileName = this.txtTempFile.Text.Trim();
            if (!File.Exists(strFileName)) //如果不檔案存在,則拋出異常
            {
                MessageBox.Show("{0} does not exist!", strFileName);
            }
            Stream s = new FileStream(strFileName, FileMode.Open);
            StreamReader sr = new StreamReader(s, Encoding.Default);
            textBox1.Text = sr.ReadToEnd().ToString();
            sr.Close();
            s.Close();
            ////////////////////
            //組合檔案內容
            string strOld = textBox1.Text.Trim();
            string strCPPName = strOld.Replace("@_funName", cppName);//"fmmm_05_del"); //更換名稱
            string strTableName = strCPPName.Replace("@_tableName",txtTableName.Text); //"TFMMM05");//更改表名稱
            string strFunDesc = strTableName.Replace("@_funDescript",txtFunDescript.Text); //"單位組管理");//功能描述
            string strCreater = strFunDesc.Replace("@_creater", txtCreater.Text);//"***");//建立者
            string strCreateTime = strCreater.Replace("@_createTime", txtCreateTime.Value.ToString("yyyy-MM-dd"));//DateTime.Now.ToString("yyyy-MM-dd"));//建立時間
            string strSQL = string.Empty;
            if (cppName.Contains("ins"))
            {
                strSQL = "insert into dbo.TFMMM07(*) values(*)";
            }
            else if (cppName.Contains("upd"))
            {
                strSQL = "update dbo.TFMMM07 set * where rec_id=@rec_id";
            }
            else if (cppName.Contains("del"))
            {
                strSQL = "delete from dbo.@_tableName where rec_id=@rec_id";
            }
            else
            {
                strSQL = "select * from dbo.@_tableName ";
            }
            string strNewSQL = strSQL.Replace("@_tableName", txtTableName.Text);
            string strResult = strCreateTime.Replace("@_sqlString", strNewSQL);
            this.textBox2.Text = strResult;            
            ////////////////////
            //寫檔案
            //執行個體化一個檔案流--->與寫入檔案相關聯
            FileStream fs = new FileStream(this.txtDestFilePath.Text+cppName+".cpp", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(this.textBox2.Text);
            sw.Flush();
            sw.Close();
            fs.Close();
        }

1。不停的賦值粘貼,修改對應的部分,直接來個檔案讀寫,把需要修改的部分用關鍵字替換掉

2。讀Stream s = new FileStream(strFileName, FileMode.Open);
            StreamReader sr = new StreamReader(s, Encoding.Default);
            textBox1.Text = sr.ReadToEnd().ToString();
3。替換,replace

4.寫:     FileStream fs = new FileStream(this.txtDestFilePath.Text+cppName+".cpp", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            sw.Write(this.textB  ox2.Text);
            sw.Flush();
            sw.Close();
            fs.Close();

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.