正文

C# 文件的输入输出程序与控制[2]2008-08-29 13:25:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yzrj/37947.html

分享到:

// 把子目录和文件添加到文件列表视图中

            lvFiles.BeginUpdate();

            lvFiles.Clear();    // 首先清除列表视图中的所有内容

            // 添加4个列表头

            lvFiles.Columns.AddRange(

                new System.Windows.Forms.ColumnHeader[] {

                        chName,

                        chSize,

                        chType,

                        chTime});

            // 首先把子目录添加到列表视图中

            for( int i=0;i<dirs.Length;i++)

            {

                items[0] = Path.GetFileName(dirs[i]);

                items[1] = "";

                items[2] = "文件夹";

                items[3] = Directory.GetLastWriteTime(dirs[i]). ToLong

DateString() + " " +Directory.GetLastWriteTime(dirs[i]). ToLongTimeString();

                lvi = new ListViewItem(items,3);

                lvFiles.Items.Add(lvi);

            }

            // 然后再把文件添加到列表视图中

            for( int i=0;i<files.Length;i++)

            {

                string ext = (Path.GetExtension( files[i] )).ToLower();

   

                // 根据不同的扩展名,来设定列表项的图标

                if( ext == ".txt")

                {

                    nImgIndex = 5;

                }

                else if( ext == ".bmp")

                {

                    nImgIndex = 6;

                }

                else if( ext == ".hlp")

                {

                    nImgIndex = 7;

                }

                else if( ext == ".exe")

                {

                    nImgIndex = 8;

                }

                else

                {

                    nImgIndex = 9;

                }

 

                items[0] = Path.GetFileName(files[i]);

                FileInfo fi = new FileInfo( files[i] );

                items[1] = fi.Length.ToString();

                items[2] = ext + "文件";

                items[3] = fi.LastWriteTime.ToLongDateString() + "  " +

                            fi.LastWriteTime.ToLongTimeString();

 

                lvi = new ListViewItem( items,nImgIndex );

                lvFiles.Items.Add( lvi );                  

            }

            lvFiles.EndUpdate();

        }

 

// <summary>

        // 打开列表视图中当前选择的文件

        // </summary>

        private void OpenFile()

        {

            if( lvFiles.SelectedItems.Count <=0 )

            {

MessageBox.Show(this,"请先选择要打开的文件!","打开文件",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

                return;

            }

            ListViewItem lvi = lvFiles.SelectedItems[0];

 

            if( (Path.GetExtension( lvi.Text )).ToLower() != ".txt" )

            {

MessageBox.Show(this,"只能打开文本文件!","打开文件错误",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

                return;

            }

           

            ContentForm fileForm = new ContentForm();

            fileForm.Text = "查看文件内容-" + lvi.Text;

string filename = tvDir.SelectedNode.FullPath + "\\" + lvFiles.SelectedItems[0].Text;

            StreamReader sr = new StreamReader( filename );

            ArrayList lines = new ArrayList();

            // 从文件中读取内容并把它们赋给显示文件内容对象框中的

            // txtContent文本框。

            while( sr.Peek() != -1 )

            {

                lines.Add( sr.ReadLine() ); // 一次读取一行数据

            }

fileForm.txtContent.Lines = (string[])lines.ToArray(Type. GetType("System.String"));

            sr.Close();

            fileForm.txtContent.Select(0,0);

            fileForm.ShowDialog(this);  // 显示对话框

        }

 

        // <summary>

        // 在当前选择的目录中创建一个新的文本文件

        // </summary>

        private void NewFile()

        {

            InputFileName formFileName = new InputFileName();

            if( formFileName.ShowDialog(this) == DialogResult.OK )

            {

                string filename = tvDir.SelectedNode.FullPath +

                    "\\" + formFileName.txtFileName.Text + ".txt";

                StreamWriter sw = new StreamWriter( filename );

                if( sw != null )

                {

                    // 创建新文件后,向其中写入测试内容

sw.Write("新创建的文本文件\n演示基本的文件输入/输出操作");

                    sw.Close();

                    ListDirsAndFiles( tvDir.SelectedNode.FullPath );

                }

            }

 

        }

 

        // <summary>

        // 删除当前选择的文件

        // </summary>

        private void DeleteFile()

        {

            if( lvFiles.SelectedItems.Count <=0 )

            {

MessageBox.Show(this,"请先选择要删除的文件!","删除文件",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);

                return;

            }

            string filename = tvDir.SelectedNode.FullPath +

                    "\\" + lvFiles.SelectedItems[0].Text;

            if( lvFiles.SelectedItems[0].ImageIndex == 3 )

            {

MessageBox.Show(this,"当前所选择的是目录,不是文件,不能进行删除!","删除文件",MessageBoxButtons.OK,MessageBoxIcon. Exclamation);

                return;

            }

            else   

                File.Delete( filename );

            ListDirsAndFiles( tvDir.SelectedNode.FullPath );

        }

 

        // <summary>

        // 在当前选择的目录中创建一个新的子目录

        // </summary>

        private void NewDirectory()

        {

            InputFileName formDir = new InputFileName();

            formDir.Text = "输入目录名称";

            formDir.label1.Text = "请输入新目录的名称:";

            if( formDir.ShowDialog(this) == DialogResult.OK )

            {

                Directory.CreateDirectory( tvDir.SelectedNode.FullPath +

                    "\\" + formDir.txtFileName.Text );


阅读(2034) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册