Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.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
如何将更改保存到Console.Title,使其超过C#中进程的生命周期?_C#_Powershell_Cmd - Fatal编程技术网

如何将更改保存到Console.Title,使其超过C#中进程的生命周期?

如何将更改保存到Console.Title,使其超过C#中进程的生命周期?,c#,powershell,cmd,C#,Powershell,Cmd,Powershell能够更改超出设置终端的进程的终端的标题文本 是Git的Powershell脚本“Posh Git”的一个示例,它在标题栏中显示Git存储库的状态,并在Powershell中工作时提示 在工作中,我们有一个控制台C#应用程序,我希望做同样的事情(设置标题窗口,但让它在应用程序的生命周期之后保持不变) 它可以从PowerShell或cmd运行 目前我将标题设置为: var path = RepositoryHelpers.TryGetContainerRootPath(work

Powershell能够更改超出设置终端的进程的终端的标题文本

是Git的Powershell脚本“Posh Git”的一个示例,它在标题栏中显示Git存储库的状态,并在Powershell中工作时提示

在工作中,我们有一个控制台C#应用程序,我希望做同样的事情(设置标题窗口,但让它在应用程序的生命周期之后保持不变)

它可以从PowerShell或cmd运行

目前我将标题设置为:

var path = RepositoryHelpers.TryGetContainerRootPath(workingDirectory);
using (var containerRepo = new Repository(path))
{
    Console.Title = $"GitSAFE - {containerRepo.Head.FriendlyName}";
}
但是,进程结束后,
Console.Title
属性将重置


我如何使用C#保存对控制台标题的更改?

从技术上讲,您无法保存所询问的更改。但是,您可以创建一个配置文件脚本,在每次启动powershell时设置标题。在windows计算机上,您可以在
C:\Users\\u your\u user\u id\Documents\WindowsPowerShell\Microsoft.PowerShell\u profile.ps1创建文件。你可以把你的命令放进去。缺点是每个用户都必须更新自己的个人资料。还有一个影响启动脚本的机器配置文件,您可以通过全局策略设置启动脚本

一种可能的缓解措施是,您可以创建一个脚本,用于检查可以使用windows基础架构配置的启动脚本。以下是我的个人资料中的一个片段:

Write-Host "Hello From Your Profile: $PsScriptRoot"
$gitDrive = $env:GitDrive
if ($gitDrive -ne "" -and $gitDrive -ne $null) {
  if (Test-Path "$($gitDrive)") {
    Write-Host "Your GitDrive is $($gitDrive)"
  }
  else {
    Write-Host "Your GitDrive is $($GitDrive) but that path does not exist."  -Fore Yellow
    Write-Host "Update the environment variable to point to your enlistment."  -Fore Yellow
    exit
  }

  if (Test-Path "$($gitDrive)AutoStart.PS1") {
    . "$($gitDrive)AutoStart.PS1"
  }
  exit
}
else {
  Write-Host "Add your GitDrive environment variable to your profile."
}
只要有一个指向git repos文件夹的环境变量,它的
AutoStart.ps1
脚本就会在每个shell上运行。YMMV但是这个
可以在我的机器上运行