【原创】使用.Net自带类实现操作文件


    首先看看界面,很明显。功能包括(1)打开一个文件(类型为文本文件.txt),在界面上显示文件路径和内容。(2)对现实的文本字体颜色做设置。(3)对字体设置。
    看了一篇文章知道在win32 API中同样存在相同的函数实现以上功能。但文章中提到如果.NET框架中有可以实现功能的类,尽量使用.NET自带的。原因有以下三点:
    (1).net的CRL是非指定平台的。就是说需要操作系统支持。跨操作系统可能会出现无法使用。
    (2).net的win32 api调用是有传递参数数据类型和类型转换的严格要求的。
    (3)调用win32 api根据编程语言的不同而不同。
    (4)调用win32 api要有权限的要求。这样会危及应用程序的安全性。
     下面把界面中各按钮的事件代码粘贴出来。
    
 private string strFileName;
        
private void btnOpen_Click(object sender, EventArgs e)
        
{
            OpenFileDialog odlg 
= new OpenFileDialog();
            odlg.CheckFileExists 
= true;
            odlg.CheckPathExists 
= true;

            odlg.DefaultExt 
= "txt";
            odlg.DereferenceLinks 
= true;
            odlg.Filter 
= "文本文件(*.txt)|*.txt|"+"所有文件|*.*";
            odlg.FilterIndex 
= 1;
            odlg.Multiselect 
= false;
            odlg.RestoreDirectory 
= true;
            odlg.ShowHelp 
= true;
            odlg.ShowReadOnly 
= false;
            odlg.ReadOnlyChecked 
= false;
            odlg.ValidateNames 
= true;
            
if (odlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            
{
                strFileName 
= odlg.FileName;
                
using (StreamReader sr = new StreamReader(odlg.OpenFile()))
                
{
                    txtFileContent.Text 
= sr.ReadToEnd();
                    txtPath.Text 
= odlg.FileName;
                }

            }

        }


        
private void Form1_Load(object sender, EventArgs e)
        
{

        }


        
private void btnSelect_Click(object sender, EventArgs e)
        
{
            
try
            
{
                FolderBrowserDialog odlgFolder 
= new FolderBrowserDialog();
                odlgFolder.RootFolder 
= Environment.SpecialFolder.MyComputer;

                
if (odlgFolder.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    txtPath.Text 
= odlgFolder.SelectedPath;
            }

            
catch (IOException ex)
            
{
                MessageBox.Show(ex.Message);
            }

        }


        
private void btnSelectColor_Click(object sender, EventArgs e)
        
{
            
int[] CustomColor ={255,65280,16711680 };
            ColorDialog cdlg 
= new ColorDialog();
            
try
            
{
                cdlg.Color 
= txtFileContent.ForeColor;
                cdlg.CustomColors 
= CustomColor;
                cdlg.AllowFullOpen 
= true;
                cdlg.AnyColor 
= false;
                cdlg.FullOpen 
= false;
                cdlg.SolidColorOnly 
= true;
                cdlg.ShowHelp 
= true;
                
if (cdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                
{
                    txtFileContent.ForeColor 
= cdlg.Color;
                    CustomColor 
= cdlg.CustomColors;
                }

                cdlg.Reset();
            }

            
catch (Exception ex)
            

            
            }

        }


        
private void btnSelectFont_Click(object sender, EventArgs e)
        
{
            FontDialog fdlg 
= new FontDialog();
            
try
            
{
                fdlg.Font 
= txtFileContent.Font;
                fdlg.Color 
= txtFileContent.ForeColor;
                fdlg.ShowColor 
= true;
                fdlg.ShowApply 
= true;
                fdlg.ShowEffects 
= true;
                fdlg.ShowHelp 
= true;
                fdlg.AllowScriptChange 
= true;
                fdlg.AllowSimulations 
= true;
                fdlg.AllowVectorFonts 
= true;
                fdlg.AllowVerticalFonts 
= true;

                fdlg.FixedPitchOnly 
= false;
                fdlg.FontMustExist 
= true;

                fdlg.MaxSize 
= 48;
                fdlg.MinSize 
= 8;

                
if (fdlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                
{
                    txtFileContent.Font 
= fdlg.Font;
                    txtFileContent.ForeColor 
= fdlg.Color;
                }

            }

            
catch (Exception ex)
            

            }

        }

     在这里也特别说明一下在“打开文件”的按钮事件里,我用了using这个语句。using主要是为了管理使用对象的。在.NET里虽然不用在每次调用对象后,显式调用其Dispose方法。但为了避免应用程序发生内存泄漏的情况,还是建议尽量在使用对象后,释放其所使用的资源。在using代码区域用{}包含。在执行到}时,CRL会自动调用对象的Dispose方法,并使用GC回收。
     所以请尽量养成习惯把图形、文件操作、端口通信、数据库连接等对象,使用using语句去管理。

posted on 2008-04-21 17:06  Daniel_xuan  阅读(418)  评论(0编辑  收藏  举报

导航