当前位置:网站首页>Rename directory in C [closed] - renaming a directory in C [closed]

Rename directory in C [closed] - renaming a directory in C [closed]

2022-07-05 07:37:00 French red wine sweet

problem :

I couldn't find a DirectoryInfo.Rename(To) or FileInfo.Rename(To) method anywhere. I can't find it anywhere DirectoryInfo.Rename(To) or FileInfo.Rename(To) Method .So, I wrote my own and I'm posting it here for anybody to use if they need it, because let's face it : the MoveTo methods are overkill and will always require extra logic if you just want to rename a directory or file : therefore , I wrote my own , I post it here , For anyone to use , If they need it , Because let's face it :MoveTo The method is excessive , If you just want to rename a directory or file , Additional logic will always be needed :

public static class DirectoryExtensions{    public static void RenameTo(this DirectoryInfo di, string name)    {        if (di == null)        {            throw new ArgumentNullException("di", "Directory info to rename cannot be null");        }        if (string.IsNullOrWhiteSpace(name))        {            throw new ArgumentException("New name cannot be null or blank", "name");        }        di.MoveTo(Path.Combine(di.Parent.FullName, name));        return; //done    }}

Solution :

Reference resources : https://stackoom.com/en/question/8UWl
原网站

版权声明
本文为[French red wine sweet]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140552567112.html