zoukankan      html  css  js  c++  java
  • 从文件路径截取需要的内容

    文件路径截取内容

    在文件的读取、保存操作时可能需要对路径执行裁剪、拼接,比如获取一个text文件的目录位置,返回指定字符串的文件名和扩展名。确定路径是否包含文件夹扩展名等等。而我们自己写的方法很多时候,在多语言处理或者截取字符串长度时容易出现问题。这篇主要是梳理System.IO.Path命名空间下提供的函数能实现对应的哪些功能,通过这些函数我们不需要自己在去写对应的截取路径代码。

     private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                string filePath = @"d:duwenlong	estpath.txt";
                FilePathTextBox.Text += $"文件路径: {filePath}
    ";
                FilePathTextBox.Text += $"更改路径字符串的扩展名: 
    ";
                FilePathTextBox.Text += System.IO.Path.ChangeExtension(filePath, "zip") + "
    ";
                FilePathTextBox.Text += $"返回指定路径字符串的目录信息: {filePath}
    ";
                FilePathTextBox.Text += System.IO.Path.GetDirectoryName(filePath) + "
    ";
                FilePathTextBox.Text += "返回指定的路径字符串的扩展名。
    ";
                FilePathTextBox.Text += System.IO.Path.GetExtension(filePath) + "
    ";
                FilePathTextBox.Text += "返回指定路径字符串的文件名和扩展名。
    ";
                FilePathTextBox.Text += System.IO.Path.GetFileName(filePath) + "
    ";
                FilePathTextBox.Text += "返回不具有扩展名的指定路径字符串的文件名。
    ";
                FilePathTextBox.Text += System.IO.Path.GetFileNameWithoutExtension(filePath) + "
    ";
                FilePathTextBox.Text += "获取指定路径的根目录信息。
    ";
                FilePathTextBox.Text += System.IO.Path.GetPathRoot(filePath) + "
    ";
                FilePathTextBox.Text += "返回随机文件夹名或文件名。
    ";
                FilePathTextBox.Text += System.IO.Path.GetRandomFileName() + "
    ";
                FilePathTextBox.Text += "创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。
    ";
                FilePathTextBox.Text += System.IO.Path.GetTempFileName() + "
    ";
                FilePathTextBox.Text += "返回当前系统的临时文件夹的路径。
    ";
                FilePathTextBox.Text += System.IO.Path.GetTempPath() + "
    ";
                FilePathTextBox.Text += "确定路径是否包括文件扩展名。
    ";
                FilePathTextBox.Text += System.IO.Path.HasExtension(filePath) + "
    ";
                FilePathTextBox.Text += "获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。
    ";
                FilePathTextBox.Text += System.IO.Path.IsPathRooted(filePath) + "
    ";
            }
    

    一般工作时从路径字符串中获取需要对应的信息,使用上面的API就可以拿到想要的东西。自己拼接或拆分路径时,可能会遇到并且需要处理多语言编码的编码问题、路径中包含不同语言奇怪字符的问题,和不同语言下。和,互换的问题等等。

  • 相关阅读:
    AE Featureclass 添加字段
    C# AE 打开本地数据
    C# DataTable操作
    IDL + AE + C#
    C#, HashTable
    KMeans Clustering
    C#打开外部程序
    二叉树删除操作(java)
    python小游戏水文
    QQ机器人
  • 原文地址:https://www.cnblogs.com/duwenlong/p/14887307.html
Copyright © 2011-2022 走看看