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;
namespace Example26
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (radioButton1.Checked)   
            {
                using (FileStream P_filestream = //创建文件流
                    new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
                {
                    object P_object = P_filestream as object; //使用as关键字转换类型
                    if (P_object != null)       //判断转换是否成功
                    {
                        MessageBox.Show("转换为 object 成功!", "提示");
                    }
                    else
                    {
                        MessageBox.Show("转换 object 失败!", "提示");
                    }
                }
            }
            if (radioButton2.Checked)
            {
                using (FileStream P_filestream = //创建文件流 
                    new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
                {
                    object P_obj = P_filestream;
                    Stream P_stream = P_obj as Stream; // 使用as关键字转换类型
                    if (P_stream != null)              //判断是否转换成功
                    {
                        MessageBox.Show("转换为 Stream 成功!", "提示");
                    }
                    else
                    {
                        MessageBox.Show("转换 Stream 失败!", "提示");
                    }
                }
            }
            if (radioButton3.Checked)
            {
                using (FileStream P_filestream = //创建文件流
                    new FileStream(@"D:\My Documents\Visual Studio 2008\Projects\Example26\Log.txt", FileMode.Create))
                {
                    object P_obj = P_filestream;    
                    string P_str = P_obj as string;  //使用as关键字转换类型
                    if (P_str != null)               //判断是否转换成功
                    {
                        MessageBox.Show("转换为 string 成功!", "提示");
                    }
                    else
                    {
                        MessageBox.Show("转换 string 失败!", "提示");
                    }
                }
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
    }
}
posted on 2011-10-17 01:33  C#_初学者  阅读(262)  评论(0编辑  收藏  举报