开发者

Selecting all files with a specific name and excluding in C#

开发者 https://www.devze.com 2023-01-30 13:45 出处:网络
Does anyone know of a way to massively exclude specific files from a c# project?I don\'t want to delete them, and I know I can set their individual properties to hidden or alter the filename with a .e

Does anyone know of a way to massively exclude specific files from a c# project? I don't want to delete them, and I know I can set their individual properties to hidden or alter the filename with a .exclude extension, but I want to know if their is a way in Visual Studio to do this.

For exa开发者_Python百科mple, if I have 1000 directories with filename img.jpeg, I want to exclude that file from every directory.

Thanks!


If you just want to remove them from your project (without deleting the actual file), open the .csproj file with an editor that can do search and replace a little better than notepad (wildcards at least) and delete the entries for these files. Save the .csproj file and reopen in Visual Studio.

Note: Visual Studio is a pretty good editor for this, since it also has a good and fast multi-file search&replace. You just have to open the .csproj as text file instead of double-clicking it.


You'll have to write an app or Visual Studio plugin to exclude the files from the projects. Personally I would just create an app that do the following:

  1. Recursively find all .csproj files in your code tree
  2. Open the cproj file by loading it into an XmlDocument object
  3. Use XPath (e.g. XmlDocument.SelectSingleNode()/SelectNodes()) to find the tag of the file in question.
  4. Remove the node and save the file back out to disk using XmlDocument.Save

So it would make a section that looks like this:

<ItemGroup>
   <EmbeddedResource Include="licenses.licx" />
   <EmbeddedResource Include="img.jpeg" />
</ItemGroup>

Look like this:

<ItemGroup>
   <EmbeddedResource Include="licenses.licx" />
</ItemGroup>

Sample XPath Code to find node:

XmlDocument csprojDoc; // this is the document you loaded your csproj XML into
XmlNamespaceManager xnm; // you'll need to build one of these because csproj files use namspaces in the XML

XmlNode excludeNode =
   csprojDoc.SelectSingleNode("//ns:ItemGroup/ns:EmbeddedResource[@Include='img.jpeg']", xnm);

excludeNode.ParentNode.ChildNodes.Remove(excludeNode);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号