How to move a file that has no file extension? C#

后端 未结 3 1879
你的背包
你的背包 2021-01-23 04:52
if (File.Exists(@\"C:\\\\Users\" + Environment.UserName + \"\\\\Desktop\\\\test\"))
{                                                                /\\
                         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-23 05:32

    You can get all files without extension in this way:

    var files = Directory.EnumerateFiles(@"C:\Users\Username\Desktop\")
        .Where(fn => string.IsNullOrEmpty(Path.GetExtension(fn)));
    

    Now you can loop them and change the extension:

    foreach (string filePath in filPaths)
    {
        string fileWithNewExtension = Path.ChangeExtension(filePath, ".txt");
        string newPath = Path.Combine(Path.GetDirectoryName(filePath), fileWithNewExtension);
        File.Move(filePath, newPath);
    }
    

    As you can see, the Path-class is a great help.


    Update: if you just want to change the extension of a single file that you already know it seems that Dasanko has already given the answer.

提交回复
热议问题