Saturday, 15 January 2011

visual studio - Easiest way to set all files with Build action 'None' to 'Copy alway to output directory' -



visual studio - Easiest way to set all files with Build action 'None' to 'Copy alway to output directory' -

i had add together many files (1000+) visual studio project. newly added files not belong project type added build action 'none' , output mode set 'don't copy'

i alter output action 'copy always'. if guys don't come smarter thought build console app und same xml transformation from

<none include="some\file.json" />

to

<none include="some\file.json"> <copytooutputdirectory>always</copytooutputdirectory> </none>

i've found solution in blog post shaun xu. feed proj file via command line arg , content/none items set re-create always.

class="lang-cs prettyprint-override">static void main(string[] args) { if (args.length < 1) { console.writeline("usage: copyallalways [project file]"); return; } var proj = args[0]; file.copy(proj, string.format("{0}.bak", proj)); var xml = new xmldocument(); xml.load(proj); var nsmanager = new xmlnamespacemanager(xml.nametable); nsmanager.addnamespace("pf", "http://schemas.microsoft.com/developer/msbuild/2003"); // add together output setting re-create var contentnodes = xml.selectnodes("//pf:project/pf:itemgroup/pf:content", nsmanager); updatenodes(contentnodes, xml, nsmanager); var nonenodes = xml.selectnodes("//pf:project/pf:itemgroup/pf:none", nsmanager); updatenodes(nonenodes, xml, nsmanager); xml.save(proj); // remove namespace attributes var content = xml.innerxml.replace("<copytooutputdirectory xmlns=\"\">", "<copytooutputdirectory>"); xml.loadxml(content); xml.save(proj); } static void updatenodes(xmlnodelist nodes, xmldocument xml, xmlnamespacemanager nsmanager) { foreach (xmlnode node in nodes) { var copytooutputdirectorynode = node.selectsinglenode("pf:copytooutputdirectory", nsmanager); if (copytooutputdirectorynode == null) { var n = xml.createnode(xmlnodetype.element, "copytooutputdirectory", null); n.innertext = "always"; node.appendchild(n); } else { if (string.compare(copytooutputdirectorynode.innertext, "always", true) != 0) { copytooutputdirectorynode.innertext = "always"; } } } }

visual-studio

No comments:

Post a Comment