简体   繁体   English

从文件完整路径中检索没有扩展名的文件名

[英]Retrieve filename Without extension from file full path

I have a list of files and need to retrieve the filename(s) (Without extension) from file full path.我有一个文件列表,需要从文件完整路径中检索文件名(无扩展名)。

I generated a list print in command prompt (with path inside intentionally);我在命令提示符下生成了一个列表打印(故意在里面有路径); and I need to replace the path with something else.我需要用其他东西替换路径。

ie: [This is the *.txt file with generated content example]即:[这是带有生成内容示例的 *.txt 文件]

C:\folder\files\img1.png  
C:\folder\files\img2.png  
C:\folder\files\img3.png  
C:\folder\files\img4.png  
...etc...

What im trying to achieve: Img1.png.我试图实现的目标:Img1.png。 or Img1或 Img1

I'm using the code bellow, but I don't know how to use it correctly yet.我正在使用下面的代码,但我还不知道如何正确使用它。

[Original Code] [原始代码]

File.WriteAllText("Path", Regex.Replace(File.ReadAllText("Path"), "[Pattern]", "Replacement"));

[The same code; [相同的代码; but modified]但修改]

File.WriteAllText(@"C:\Folder\Files\print.txt", Regex.Replace(File.ReadAllText(@"C:\Folder\Files\print.txt"), "[C:\folder\files\]", "Copy "));

Couldn't you just use GetFileName and provide the string-value(s)?你不能只使用GetFileName并提供字符串值吗? You can append the output to a string-builder and just rewrite the file.您可以将输出附加到字符串构建器并重写文件。

This is how you can use it.这就是您可以使用它的方式。 You do not need to use RegEx in this case, because the string which you want to replace is well defined:在这种情况下,您不需要使用 RegEx,因为您要替换的字符串是明确定义的:

File.WriteAllText(@"C:\Folder\Files\print.txt", File.ReadAllText(@"C:\Folder\Files\print.txt").Replace(@"C:\folder\files\", "Copy "));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM