Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Nuget Powershell重命名内容文件_C#_Powershell_Nuget - Fatal编程技术网

C# Nuget Powershell重命名内容文件

C# Nuget Powershell重命名内容文件,c#,powershell,nuget,C#,Powershell,Nuget,我希望重命名通过nuget软件包加载的文件: 我知道我可以得到这样的文件: $f = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "MyFile.cs.custom" } $f.Delete() 我也知道我可以像这样删除该文件: $f = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where {

我希望重命名通过nuget软件包加载的文件:

我知道我可以得到这样的文件:

$f = $project.ProjectItems | ForEach-Object { $_.ProjectItems } | where { $_.Name -eq "MyFile.cs.custom" } 
$f.Delete()
我也知道我可以像这样删除该文件:

$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文件的project startup对象更改为项目中不存在的类,从而解决了这个问题。

我怀疑ProjectItem是否属于FileInfo类型,请尝试以下方法:

$project.ProjectItems | ForEach { $_.ProjectItems } | 
    Where { $_.Name -eq "MyFile.cs.custom" } |  
    Foreach {Rename-Item $_.Name ($_.FileNames -replace "\.custom", "")}
我认为您需要在该项上找到一个与文件的全名(路径)对应的属性(用作重命名项的路径),除非您可以保证PowerShell已cd刻录到文件的包含目录中

另外,请注意
-replace
使用正则表达式元字符


请注意,
FileNames
可以在单个字符串中返回多个文件名

我怀疑项目项的类型是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

我们的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

我希望它对某些人有用,至少对我有用。在这段代码中,我正在根文件夹中查找文件夹“FolderConfig”(例如C:\projects\SomeProject\FolderConfig\),然后重命名文件。您可以按文件夹“FolderConfig”删除筛选器,并在所有项目中搜索这些文件

$project.ProjectItems | where { $_.Name -eq "FolderConfig" } |
ForEach { $_.ProjectItems } |  where { $_.Name -eq "Test.config"} |
ForEach { $_.Name = "NewTest.config" }

我希望它对某些人有用,至少对我有用。在这段代码中,我正在根文件夹(例如C:\projects\SomeProject\FolderConfig\)中查找文件夹“FolderConfig”,然后重命名文件。您可以按文件夹“FolderConfig”删除筛选器,并在所有项目中搜索这些文件

$project.ProjectItems | where { $_.Name -eq "FolderConfig" } |
ForEach { $_.ProjectItems } |  where { $_.Name -eq "Test.config"} |
ForEach { $_.Name = "NewTest.config" }

-verbose
放在rename item cmdlet上,以便查看它试图从和重命名到的内容。我怀疑您需要的不仅仅是
Name
属性。项目上是否有
Fullname
Path
属性?将最后一个Foreach更改为
Get Member
,并查看此DTE对象的属性i、 e.何处{…}|获取成员根据此MSDN页面-该属性名为
FileNames
。作为rename item cmdlet的install.ps1 filePut
-verbose
的一部分,该属性仍然无法正常工作,因此您可以查看它试图从和到重命名的内容。我怀疑您需要的不仅仅是
Name
属性。是否有
Fullname
项的路径
属性?将最后一个Foreach更改为
获取成员
,然后查看此DTE对象上有哪些属性,即{…}获取成员的位置根据此MSDN页面-该属性名为
文件名
。作为install.ps1文件的一部分,该属性仍然无法正常工作