[英]Nuget Powershell Rename content file
我想重命名通过nuget包加载的文件:
我知道我可以得到这样的文件:
$f = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "MyFile.cs.custom" }
我也知道我可以像这样删除该文件:
$f.Delete()
我只是在寻找重命名该文件的语法。 我试着只是做:
$project.ProjectItems | ForEach { $_.ProjectItems } | Where { $_.Name -eq "MyFile.cs.custom" } | Foreach {Rename-Item $_.Name ($_.FileNames -replace "\.custom", "")}
那没有用
整个install.ps1文件为:
param($installPath, $toolsPath, $package, $project)
#save the project file first - this commits the changes made by nuget before this script runs.
$project.Save()
$project.ProjectItems | ForEach { $_.ProjectItems } | Where { $_.Name -eq "MyFile.cs.custom" } | Foreach {Rename-Item $_.Name ($_.FileNames -replace "\.custom", "")}
$project.Save()
最后,我通过将csproj文件的项目启动对象更改为项目中尚不存在的类来解决此问题。
我希望它对某人有用,至少对我有用。 在这段代码中,我正在根文件夹中查找文件夹“ FolderConfig”(例如C:\\ projects \\ SomeProject \\ FolderConfig \\”,然后重命名文件。您可以通过文件夹“ FolderConfig”删除过滤器并在以下位置搜索这些文件所有项目。
$project.ProjectItems | where { $_.Name -eq "FolderConfig" } |
ForEach { $_.ProjectItems } | where { $_.Name -eq "Test.config"} |
ForEach { $_.Name = "NewTest.config" }
我怀疑ProjectItems的类型为FileInfo,请尝试以下操作:
$project.ProjectItems | ForEach { $_.ProjectItems } |
Where { $_.Name -eq "MyFile.cs.custom" } |
Foreach {Rename-Item $_.Name ($_.FileNames -replace "\.custom", "")}
我认为您将需要在该项目中找到一个与文件全名(路径)相对应的属性(用作重命名项的路径),除非您可以确保将PowerShell CD装入文件的包含目录。
另外,请注意-replace
使用正则表达式元字符。
请注意, FileNames
可能在单个字符串中返回多个文件名。
我们的install.ps1文件应如下所示。
param($installPath, $toolsPath, $package, $project)
$file1 = $project.ProjectItems.Item("test.exe")
$file2 = $project.ProjectItems.Item("test.config")
// set 'Copy To Output Directory' to 'Copy if newer'
$copyToOutput1 = $file1.Properties.Item("CopyToOutputDirectory")
$copyToOutput1.Value = 2
$copyToOutput2 = $file2.Properties.Item("CopyToOutputDirectory")
$copyToOutput2.Value = 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.