zoukankan      html  css  js  c++  java
  • C#文件路径获取函数和文件名字获取函数

    1. 获取绝对文件路径

    System.IO.Path.GetFullPath(string path)

    string fileName = "myfile.ext";
    string path1 = @"mydir";
    string path2 = @"\mydir";
    string fullPath;

    fullPath = Path.GetFullPath(path1);
    fullPath = Path.GetFullPath(fileName);
    fullPath = Path.GetFullPath(path2);

    2. 获取文件名字(得到指定路径内的文件名,不包括扩展名)

    System.IO.Path.GetFileNameWithoutExtension(string path)
    string fileName = @"C:\mydir\myfile.ext";
    string path = @"C:\mydir\";
    string result;
    result = Path.GetFileNameWithoutExtension(fileName);
    result = Path.GetFileName(path);

    3.获得包含 path 目录信息的string 或者为空引用

    System.IO.Path.GetDirectoryName(string path)

    string fileName = @"C:\mydir\myfile.ext";
    string path = @"C:\mydir\";
    string rootPath = @"C:\";
    string directoryName;
       
    directoryName = Path.GetDirectoryName(fileName);
    directoryName = Path.GetDirectoryName(path);
    directoryName = Path.GetDirectoryName(rootPath);

    4.合并两个路径字符串。

    System.IO.Path.Combine(String path1, String path2)

  • 相关阅读:
    mysql案例 ~ 监控以及如何避免从库延迟问题
    mysql基础~开发规范
    k8s系列~mgr的应用
    k8s系列~docker mysql
    常用知识点(一)
    Windows下查看进程状态/信息
    Lua入门(一)
    Lua简介
    .NET&C#的异常处理
    数据库中的锁
  • 原文地址:https://www.cnblogs.com/greenteaone/p/1882044.html
Copyright © 2011-2022 走看看