GTX_AI 发表于 2020-10-1 18:50:02

C# string操作

C# string操作,小技巧:
string Format = ImageFormat.Substring(ImageFormat.IndexOf(".") + 1);
string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名。
string str2=Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径。
string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
String[] files = System.IO.Directory.GetFiles(path) //返回指定目录下的文件名
string str = System.IO.Path.GetFileNameWithoutExtension(path);//返回不具有扩展名的指定路径字符串的文件名参考:
【1】C# 获取文件路径
【2】C#使用System.IO.Path获取文件路径、文件名

GTX_AI 发表于 2021-3-14 12:38:41

            //一个文件目录
            string filePath = "C:\\bin\\files\\test.xml";
            Console.WriteLine("该文件的目录:"+filePath);

            string str = "获取文件的全路径:" + Path.GetFullPath(filePath);   //-->C:\bin\files\test.xml
            Console.WriteLine(str);
            str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\bin\files
            Console.WriteLine(str);
            str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);//-->test.xml
            Console.WriteLine(str);
            str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->test
            Console.WriteLine(str);
            str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml
            Console.WriteLine(str);
            str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\

Halcom 发表于 2021-5-9 11:06:23

GTX_AI 发表于 2021-3-14 12:38


string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string extension = ".log";
filePath += @"\Error Log\" + extension;
if (!Directory.Exists(filePath))
{
      Directory.CreateDirectory(filePath);
}
页: [1]
查看完整版本: C# string操作