zoukankan      html  css  js  c++  java
  • C# 文件与路径操作

    OpenFileDialog

    private void btnOpenFileDialog_Click(object sender, EventArgs e)
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = @"C:SeeSharpLYH";                   //设置起始路径
                openFileDialog.Title = "打开文件";                                      //对话框标题
                openFileDialog.Filter = "JXI Vector file|*.iq|实信号|*.if";             //设置过滤器
                openFileDialog.FilterIndex = 1;                                         //默认过滤器中类型
                openFileDialog.Multiselect = false;                                     //是否允许多选
                openFileDialog.RestoreDirectory = true;                                 //再次打开是否记住上次路径
                if (openFileDialog.ShowDialog() != DialogResult.OK) return;             //打开对话框,对话框结果不为OK则返回
                if (openFileDialog.Multiselect)
                {
                    textBox1.Text = "";
                    foreach (var item in openFileDialog.FileNames) { textBox1.AppendText(item + "
    "); }
                }
                else
                {
                    textBox1.Text = openFileDialog.FileName;
                }
            }

    SaveFileDialog

    private void btnSaveFileDialog_Click(object sender, EventArgs e)
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Title = "打开文件";                                      //对话框标题
                saveFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";  //设置过滤器
                saveFileDialog.FilterIndex = 0;                                         //默认过滤器中类型
                saveFileDialog.RestoreDirectory = true;                                 //保存对话框是否记忆上次打开的目录
                if (saveFileDialog.ShowDialog() != DialogResult.OK) return;             //打开对话框,对话框结果不为OK则返回
                textBox1.Text = saveFileDialog.FileName;
            }

    FolderBrowserDialog

    private void btnFolderBrowserDialog_Click(object sender, EventArgs e)
            {
                FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
                folderBrowserDialog.SelectedPath = @"C:SeeSharpLYHSeeSharpTools";        //设置默认路径
                folderBrowserDialog.Description = "请选择一个文件夹";                       //设置提示信息
                if (folderBrowserDialog.ShowDialog() == DialogResult.OK)                    //打开对话框 //获取选中结果
                {
                    textBox1.Text = folderBrowserDialog.SelectedPath;
                }
            }

    获取特殊目录

    static void SpecialDirectory()
            {
                string dir;            
    
                //获取当前目录(即该进程从中启动的目录)的完全限定路径。
                dir = Environment.CurrentDirectory;
                //备注 按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:/”)。
                //如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径(如“C:/mySubDirectory”)。
                dir = Directory.GetCurrentDirectory();
    
                //获取当前目录的上级目录
                dir = Path.GetFullPath("..");
    
                //桌面路径
                dir = "桌面路径:" 
                    + Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
    
                //ProgramData文件夹路径
                dir = "ProgramData文件夹路径:" 
                    + Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    
                //我的文档文件夹路径
                dir = "我的文档文件夹路径" 
                    + Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
               
                //获得当前可执行的exe文件名
                dir = "Process.GetCurrentProcess().MainModule.FileName:" 
                    + Process.GetCurrentProcess().MainModule.FileName;
               
                //获取基目录,它由程序集冲突解决程序用来探测程序集。
                dir = "AppDomain.CurrentDomain.BaseDirectory:" 
                    + AppDomain.CurrentDomain.BaseDirectory;
              
                //获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。(如:D:/project/集团客户短信服务端/bin/Debug)
                dir = "System.Windows.Forms.Application.StartupPath:" 
                    + System.Windows.Forms.Application.StartupPath;
               
                //获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
                dir = "System.Windows.Forms.Application.ExecutablePath:" 
                    + System.Windows.Forms.Application.ExecutablePath;
               
                //获取或设置包含该应用程序的目录的名称。 
                dir = "AppDomain.CurrentDomain.SetupInformation.ApplicationBase:" 
                    + AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                
                Console.ReadKey();
            }

    获取当前目录及其父目录

            /// <summary>
            /// 获取当前目录及当前目录第n级的父目录
            /// </summary>
            /// <param name="level">父目录级别,大于等于0</param>
            /// <returns></returns>
            public static string CurrentDirectory(int level=0)
            {
                string str = Environment.CurrentDirectory;
                for (int i = 0; i < level; i++)
                    str = str.Substring(0, str.LastIndexOf(@""));
                return str;
            }

    获取已知路径中的元素

    static void pth()
            {
                string dir;
                string filePath = "C:\JiYF\BenXH\BenXHCMS.xml";       
                dir = "获取文件的全路径:" + Path.GetFullPath(filePath);//-->C:JiYFBenXHBenXHCMS.xml           
                dir = "获取文件所在的目录:" + Path.GetDirectoryName(filePath);//-->C:JiYFBenXH           
                dir = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);//-->BenXHCMS.xml           
                dir = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->BenXHCMS           
                dir = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath);//-->.xml            
                dir = "获取路径的根目录:" + Path.GetPathRoot(filePath);//-->C:
                Console.ReadKey();
            }

    获取指定路径下的文件与子目录

    static void Main(string[] args)
            {
                // 获取目标目录信息
                string selectedDir = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory); ;
                Console.WriteLine("Open : " + selectedDir+"
    ");
                DirectoryInfo root = new DirectoryInfo(selectedDir);
    
                // 从目标目录信息中获取目录中文件信息
                FileInfo[] fileInfo = root.GetFiles("*.txt");
                Console.WriteLine("*.txt file list start...");
                foreach (var file in fileInfo)
                {
                    Console.WriteLine("new txt file:" + file.FullName);
                }
    
                // 从目标目录信息中获取子目录信息
                Console.WriteLine("
    
     sub dir list start...");
                DirectoryInfo[] subDirList = root.GetDirectories();
                foreach (var subDir in subDirList)
                {
                    Console.WriteLine("find sub dir:" + subDir.FullName);
                }
    
                Console.WriteLine("
    Done...");
                Console.Read();
            }

    代码运行结果如下

    image

    文件操作

    private void FileOperations()
            {
                string sourcePath = "sourcePath";
                string destpath = "destpath";
                
                //复制文件到目标路径
                File.Copy(sourcePath, destpath);
    
                //删除制定文件
                File.Delete(sourcePath);
    
                //移动文件到目标路径【重命名的实现方法】
                //sourcePath与destpath在不同目录下则为文件的移动,在同一目录下则为文件的重命名
                File.Move(sourcePath, destpath);
    
                //打开一个文本文件*.txt ,读取文件中数据,然后关闭该文件
                File.ReadAllText(sourcePath);
    
                //创建一个文件,向其中写入数据,如果此路径下有同名文件则会覆盖
                //【PS:对文件进行写入操作,如果路径下有同名文件则会进行覆盖,所以最好进行一次判断,跟用户交互一下在进行覆盖】
                File.WriteAllText(sourcePath, "要写入文件的字符串"); 
            }
    参考 https://www.cnblogs.com/Zhang-silence/p/6866722.html 
  • 相关阅读:
    发布一个扩展Repeater的模板控件,带自动分页功能
    webservice 测试窗体只能用于来自本地计算机的请求
    FCKeditor编辑器中设置默认文本行高和字体大小
    程序员的个人性格
    程序设计模式的有趣解释-追MM
    集锦一
    UML简介(原创)
    一位IT从业人员的心路历程
    一个初级测试工程师的工作总结
    "与熊共舞"(转载)
  • 原文地址:https://www.cnblogs.com/lyh523329053/p/9215130.html
Copyright © 2011-2022 走看看