根据目录,子目录和文件创建动态按钮 [英] Create Dynamic buttons based on directories, subdirectories and files

查看:77
本文介绍了根据目录,子目录和文件创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

早上好,

我是C#和WPF领域的新手.我需要创建一个基于动态按钮/窗体(窗口)的触摸友好型(它将在Surface 4设备上运行)文档浏览器.

I'm a newbie in the world of C# and WPF. I need to create a touch friendly (it will be run on a surface 4 device)document browser based on dynamic buttons / forms(windows).

目录的结构如下:

C:\ dir

C:\ dir1 \ a.pdf

C:\dir1\a.pdf

c:\ dir1 \ dir2 \ b.pdf

c:\dir1\dir2\b.pdf

...

我有一些代码,但是目录的路径是硬编码的",我希望它在xml文件或ini文件中

I have some code but the paths of the directories are 'hard coded' and I would like it on an xml file or ini file

           int x = 5;
          整数y = 10;
           const int dy = 5;

           DirectoryInfo dInfo =新的DirectoryInfo(@"C:\ dir1");
           DirectoryInfo [] dir = dInfo.GetDirectories();

           foreach(目录中的DirectoryInfo DIR)
           {
                             Button btn = new Button();
                             btn.Text = DIR.Name;
                             btn.Left = x;
                             btn.Top = y;
                             btn.Width = 350;
                             btn.Height = 75;
                             btn.Click + = handleclick;
                             btn.Tag = DIR.FullName;
                             this.Controls.Add(btn);
                             y + = btn.Height + dy;
           }
       }

            int x = 5;
            int y = 10;
            const int dy = 5;

            DirectoryInfo dInfo = new DirectoryInfo(@"C:\dir1");
            DirectoryInfo[] dir = dInfo.GetDirectories();

            foreach (DirectoryInfo DIR in dir)
            {
                Button btn = new Button();
                btn.Text = DIR.Name;
                btn.Left = x;
                btn.Top = y;
                btn.Width = 350;
                btn.Height = 75;
                btn.Click += handleclick;
                btn.Tag = DIR.FullName;
                this.Controls.Add(btn);
                y += btn.Height + dy;
            }
        }

有人可以帮我吗?

谢谢

推荐答案

在Visual Studio中,在解决方案资源管理器中,单击属性"->设置并定义一个名为(例如)"MyFolder"的新设置,并在其中写入"C:\ Dir1".作为价值.在内部,这实际上将保存为app.config中的XML. 文件,当您编译项目时,该文件将被复制到yourProgram.exe.config.

In Visual Studio, in Solution Explorer, click on Properties -> Settings and define a new Setting called (for instance) "MyFolder", and write "C:\Dir1" as the value. Internally, this will actually be saved as XML inside the app.config file, which will be copied to yourProgram.exe.config when you compile the project.

完成此操作后,就可以从代码中恢复以XML格式存储在.config文件中的值,如下所示:

Once you've done that, from you code you can recover the value stored as XML in the .config file like this:

字符串文件夹= Properties.Settings.Default.MyFolder;

string folder = Properties.Settings.Default.MyFolder;


这篇关于根据目录,子目录和文件创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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