Search code examples
visual-studio-extensions

Can a 'linked'-file be renamed programatically via an Extension


I've written an Extension that, among many other things, renames files based on the Types they contain.

This works fine for files in the directory-tree under the csproj-file -- I find the ProjectItem entry for the file and change its name.

For 'linked'-files (those not in the directory-tree) I can rename the file (via File.Move()) but haven't found a way to programatically modify the csproj-file (after the rename the csproj-file has to be modified manually).

If this is something that can be done I'd appreciate a pointer to the docs showing how to implement the functionality.


Solution

  • The easiest solution for me was to modify the csproj-file.

    1. Open, read whole file, close.
    2. Verify that file I want to rename (e.g. xxx.cs) only occurs in 1 directory (if it occurs in multiple directories the change has to be done manually.)
    3. Make change
    4. Open, write whole file, close
    • For an SDK project the change is applied immediately.
    • For a non-SDK project the change is applied after responding to the prompt that the csproj-file has been modified.