以下是根据“[Description("”,批量添加“System.ComponentModel”引用
[System.String]$dir= "D:\SVN\MyProject\Erp"
function Deal ([System.IO.FileInfo]$file)
{
[System.Text.Encoding]$enc = [System.Text.Encoding]::UTF8
[System.String]$str = [System.IO.File]::ReadAllText($file.FullName,$enc);
if($str.Contains("[Description(""") -and !$str.Contains("using System.ComponentModel;"))
{
Write-Host $file.FullName.Replace($dir,"")
$idx=$str.IndexOf("using System")
Write-Host $idx
if ($idx -gt -1)
{
$sp = "`r`n";
$idx=$str.IndexOf($sp);
if($idx -eq -1)
{
$sp = "`r";
$idx = $str.IndexOf("`r");
if($idx -eq -1)
{
$sp = "`n";
$idx = $str.IndexOf("`n");
}
}
Write-Host $idx
if($idx -eq -1)
{
$str = "using System.ComponentModel;`r`n"+$str;
}
else {
$str = $str.Substring(0,$idx)+$sp+"using System.ComponentModel;"+$str.Substring($idx);
}
}
else {
$str = "using System.ComponentModel;`r`n"+$str;
}
#[System.IO.File]::WriteAllText($file.FullName,$str,$enc);
}
}
Get-ChildItem -Path $dir -File -Recurse -Filter *.tt|ForEach-Object{Deal($_)}
#$file = New-Object System.IO.FileInfo -ArgumentList "D:\SVN\MyProject\Erp\Common\Models\UserInfo.cs"
#Deal($file)