如何使用c#删除和打开文件浏览器中的文件? [英] How to delete and open files in file browser using c#?

查看:92
本文介绍了如何使用c#删除和打开文件浏览器中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧:



i在c#

中使用treeview制作文件浏览器代码它显示输入的文件和目录目录/文件



但我不知道删除并打开所选文件或目录?

请帮帮我... 。



这里是代码



Hi all :)

i make a code of file browser using treeview in c#
it displays the files and directories of the input dir/file

but i don't have an idea of deleting and opening the selected file or directory ?
help me please ...

here is the code

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 WindowsFormsApplication14
{
    public partial class Form1 : Form
    {
        string subpathdir;
        public Form1()
        {
            InitializeComponent();
        }

        public void PopulateTreeView(string dirctory, TreeNode parentnode)
        {

            string[] directories = Directory.GetDirectories(dirctory);
            try
            {

                if (directories.Length != 0)
                {
                    foreach (string dir in directories)
                    {

                        subpathdir = Path.GetFileNameWithoutExtension(dir);


                        TreeNode mynode = new TreeNode(subpathdir);
                        parentnode.Nodes.Add(mynode);
                        PopulateTreeView(dir, mynode);

                    }
                }
            }



            catch(UnauthorizedAccessException)
            {
                parentnode.Nodes.Add("access denied");


            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
            if (Directory.Exists(textBox1.Text))
            {
                treeView1.Nodes.Add(textBox1.Text);
                PopulateTreeView(textBox1.Text, treeView1.Nodes[0]);
            }
            else
                MessageBox.Show(textBox1.Text + "could not be found ", "directory not found", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }
    }

}

推荐答案

删除很容易: File.Delete [ ^ ]会为你做这件事。



打开文件更难 - 更难。这并不难: File.ReadAllBytes [ ^ ]将为您提供文件中的所有内容。

但是你用它做什么呢? .NET中没有标准的显示此数据,因为每个应用程序都以不同的方式存储数据,并以自己的方式解释数据:因为Excel不易读取Excel电子表格是有意义的,更不用说Gimp了!反之亦然:Excel也不喜欢打开Corel Paintshop Pro文件!



你需要坐下来准确计算你想要对文件做什么打开它时的内容,然后开始考虑您想要或需要支持的文件类型。
Delete is easy: File.Delete[^] will do that for you.

Open files is harder - a lot harder. It's not difficult to do: File.ReadAllBytes[^] will get you everything in the file, for example.
But what do you do with it then? There is no standard "display this data" in .NET because every application stores its data differently, and interprets that data in its own way: what makes sense as an Excel spreadsheet is not readily readable by Word, let alone Gimp! And vice versa: Excel doesn't like to open Corel Paintshop Pro files either!

You need to sit down and work out exactly what you want to do with the file content when you have opened it, and then start thinking about what file types you want or need to support.


这篇关于如何使用c#删除和打开文件浏览器中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆