zoukankan      html  css  js  c++  java
  • C#选取文件夹的对话框

    首先要说明一下:

    添加引用:   System.Design 

    此文件在下面的位置 

    C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Design.dll

    有些人说在添加引用的面板中并找不到这个引用.这是由于你的目标框架设置错误

    在 解决方案管理器上  右键 "属性" --> 发布 --> 目标框架 修改为 .Net Framework 4.0  (没有那个client  )

    这样你再在添加引用面板中就可以找到这个引用了.

    添加这两个引用

    using System.Windows.Forms;
    using System.Windows.Forms.Design;


     

    ==================新建一个FolderDialog类 ==============
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.Design;
       public class FolderDialog : FolderNameEditor
        {
            FolderNameEditor.FolderBrowser fDialog = new FolderNameEditor.FolderBrowser();
            public FolderDialog()
            {
            }
            public DialogResult DisplayDialog()
            {
                return DisplayDialog("请选择一个文件夹");
            }
            public DialogResult DisplayDialog(string description)
            {
                fDialog.Description = description;
                return fDialog.ShowDialog();
            }
            public string Path
            {
                get { return fDialog.DirectoryPath; }
            }
            ~FolderDialog() { fDialog.Dispose(); }
        }
    --------------调用--------------------
                FolderDialog openFolder = new FolderDialog();
                if (openFolder.DisplayDialog() == DialogResult.OK)
                {
                    textBox1.Text = openFolder.Path.ToString();
                }
                else
                {
                    textBox1.Text = "你没有选择目录";
                }
                DirectoryInfo dirInfo = new DirectoryInfo(textBox1.Text);
                FileInfo[] files = dirInfo.GetFiles();
                foreach (FileInfo filename in files)
                {
                    listBox1.Items.Add(filename);
                }

    下面的例子是一个获取文件夹内的所有文件的

    using System;
    using System.IO;
    namespace ConsoleApplication7
    {
     /// <summary>
     /// Class1 的摘要说明 
     /// </summary>
     class Class1
     {
      /// <summary>
      /// 应用程序的主入口点 
      /// </summary>
      [STAThread]
      static void Main(string[] args)
      {
       string dirp=@"d:\\d";
       DirectoryInfo mydir = new DirectoryInfo(dirp);
       foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos())
       {
        if (fsi is FileInfo)
        {
         FileInfo fi = (FileInfo)fsi;
         string x=System.IO.Path.GetDirectoryName(fi.FullName);
         Console.WriteLine(x);
         string s=System.IO.Path.GetExtension(fi.FullName);
         string y=System.IO.Path.GetFileNameWithoutExtension(fi.FullName);
         Console.WriteLine(y);
               if(s==".jpg")
               {
               System.IO.File.Copy(fi.FullName,x+@"\oo"+fi.Name); //在原文件名前加上OO
               System.IO.File.Delete(fi.FullName);
               }
        }
        
       }
       Console.WriteLine("成功");
       Console.ReadLine();
      }
     }
  • 相关阅读:
    vue项目搭建
    iview在ie9及以上的兼容问题解决方案
    中山大学校队内部选拔赛试题试题2【New Year Gift】--------2015年2月8日
    中山大学校队选拔赛第二试题试题3【Compressed suffix array】-------2015年2月8日
    ZOJ2812------2015年2月4日
    C++STL泛型编程基础知识讲解--------2015年2月3日
    中山大学校队选拔赛第一章题4【简单数迷Simple Kakuro】-------2015年1月28日
    UVALive
    UVA11375【火柴拼数Matches】-------2015年1月27日
    递推关系的运用加简单DP【UVA11137Ingenuous Cubrency】-------2015年1月27日
  • 原文地址:https://www.cnblogs.com/lujin49/p/2461499.html
Copyright © 2011-2022 走看看