網頁

2011年10月2日 星期日

[C#] C#調用記事本

C#中如何调用记事本

  
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; //for FileInfo
using System.Diagnostics; //for notepad.exe process

namespace File
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //
            //記事本建檔寫檔
            //
            string filename = Environment.CurrentDirectory + @"\Test.txt";
            FileInfo f = new FileInfo(filename);
            StreamWriter sw = f.CreateText();
            sw.Write("記事本內容");
            sw.Flush();
            sw.Close();

            //
            //外部程式 記事本讀檔
            //
            System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();

            //設定外部程式名(記事本) 
            Info.FileName = "notepad.exe";

            //設置外部程式的啟動參數檔名 

            Info.Arguments = filename;

            //設定置外部檔案位置 (目前位置)

            //Info.WorkingDirectory = Environment.CurrentDirectory;

            try
            {
                // 
                //啟動外部程式
                // 
                System.Diagnostics.Process.Start(Info);
            }
            catch
            {
                MessageBox.Show("系統找不到檔案", "錯誤提示!");
                return;
            }


        }
    }
}

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。