本文介绍了从多个子目录中的多个文件中删除扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



我想从主文件夹中所有子文件夹中的所有可用文件中删除扩展名。



示例:下面是文件夹结构。



Dear All,

I want to remove the extension from all the available files in all the sub folders in a Main folder.

Example : Below is the folder structure.

MainDirectory
|
|---> SubDirectory1/ abc1.config.txt / abc2.config.txt /  abc3.config.txt /
|
|---> SubDirectory2/ abc1.config.txt / abc2.config.txt /  abc3.config.txt /
|
|---> SubDirectory3/ abc1.config.txt / abc2.config.txt /  abc3.config.txt /



所以,我想要从所有的.txt扩展名中删除以c#编程的所有子目录中的文件,这样我最后只会有配置文件。



请建议或提供一些c#代码。



提前致谢,



我尝试过:



我已经尝试过Path.ChangeExtension,它实际上用null替换了扩展名,但它不适用于物理文件。我的意思是,它只是以逻辑方式给用户赋值。


So, I want ".txt" extension to be removed from all the files in all the sub directories programmatically in c#, so that I will be having only config files at the end.

Please suggest or provide some piece of c# code.

Thanks in advance,

What I have tried:

I have tried Path.ChangeExtension which actually replaces the extension with null, but it does not apply for physical file. I mean, it just gives the value to the user in logical way.

推荐答案


string[] files = Directory.GetFiles(@"D:\Temp\", "*.txt", SearchOption.AllDirectories);
foreach (string file in files)
    {
    File.Move(file, file.Substring(0, file.Length - 4));
    }


string path = @"C:\Users\Whoever\Desktop\SubDirectory1\abc1.txt.config";

string newpath = Path.ChangeExtension(path, "");
File.Move(path, newpath);

虽然这个有效,但是以这种方式操作文件扩展名的需要让我想知道是否有更简单的方法可以做任何事情你在这里做什么。

While this "works," the need to manipulate file extensions in this way makes me wonder if there isn't a simpler way to do whatever it is you are doing here.


这篇关于从多个子目录中的多个文件中删除扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 12:09