Hello Mat

 找回密码
 立即注册
查看: 8620|回复: 4

C# string操作

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2020-10-1 18:50:02 | 显示全部楼层 |阅读模式
C# string操作,小技巧:
  1. string Format = ImageFormat.Substring(ImageFormat.IndexOf(".") + 1);
  2. string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名。
  3. string str2=Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径。
  4. string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
  5. string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
  6. string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
  7. string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
  8. string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
  9. String[] files = System.IO.Directory.GetFiles(path) //返回指定目录下的文件名
  10. string str = System.IO.Path.GetFileNameWithoutExtension(path);//返回不具有扩展名的指定路径字符串的文件名
复制代码
参考:
【1】C# 获取文件路径
【2】C#使用System.IO.Path获取文件路径、文件名

回复

使用道具 举报

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
 楼主| 发表于 2021-3-14 12:38:41 | 显示全部楼层
  1.             //一个文件目录
  2.             string filePath = "C:\\bin\\files\\test.xml";
  3.             Console.WriteLine("该文件的目录:"+filePath);

  4.             string str = "获取文件的全路径:" + Path.GetFullPath(filePath);   //-->C:\bin\files\test.xml
  5.             Console.WriteLine(str);
  6.             str = "获取文件所在的目录:" + Path.GetDirectoryName(filePath); //-->C:\bin\files
  7.             Console.WriteLine(str);
  8.             str = "获取文件的名称含有后缀:" + Path.GetFileName(filePath);  //-->test.xml
  9.             Console.WriteLine(str);
  10.             str = "获取文件的名称没有后缀:" + Path.GetFileNameWithoutExtension(filePath); //-->test
  11.             Console.WriteLine(str);
  12.             str = "获取路径的后缀扩展名称:" + Path.GetExtension(filePath); //-->.xml
  13.             Console.WriteLine(str);
  14.             str = "获取路径的根目录:" + Path.GetPathRoot(filePath); //-->C:\
复制代码

回复 支持 反对

使用道具 举报

1294

主题

1520

帖子

110

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22633
发表于 2021-5-9 11:06:23 | 显示全部楼层

string filePath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string extension = ".log";
filePath += @"\Error Log\" + extension;
if (!Directory.Exists(filePath))
{
      Directory.CreateDirectory(filePath);
}
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-25 20:38 , Processed in 0.238049 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表