@@ -26,6 +26,39 @@ public static string RenameProjectFile(string projectFilePath, string newName)
2626 return newFilePath ;
2727 }
2828
29+ /// <summary>
30+ /// Renames the parent directory if its name matches the old project name.
31+ /// </summary>
32+ /// <param name="projectFilePath">Full path to the .csproj file.</param>
33+ /// <param name="oldName">The old project name to match against.</param>
34+ /// <param name="newName">The new project name.</param>
35+ /// <returns>The new full path to the project file after directory rename, or the original path if no rename occurred.</returns>
36+ public static string RenameParentDirectoryIfMatches ( string projectFilePath , string oldName , string newName )
37+ {
38+ var projectDirectory = Path . GetDirectoryName ( projectFilePath ) ;
39+ var parentDirectory = Directory . GetParent ( projectDirectory ) ;
40+
41+ if ( parentDirectory == null )
42+ {
43+ return projectFilePath ;
44+ }
45+
46+ var directoryName = new DirectoryInfo ( projectDirectory ) . Name ;
47+
48+ // Only rename if directory name matches the old project name
49+ if ( ! directoryName . Equals ( oldName , StringComparison . OrdinalIgnoreCase ) )
50+ {
51+ return projectFilePath ;
52+ }
53+
54+ var newDirectoryPath = Path . Combine ( parentDirectory . FullName , newName ) ;
55+ Directory . Move ( projectDirectory , newDirectoryPath ) ;
56+
57+ // Return the new project file path
58+ var fileName = Path . GetFileName ( projectFilePath ) ;
59+ return Path . Combine ( newDirectoryPath , fileName ) ;
60+ }
61+
2962 /// <summary>
3063 /// Updates the RootNamespace and AssemblyName elements in a project file
3164 /// if they match the old project name.
0 commit comments