powershell 编程

UPDATE: Why my own MacGyver solution was brilliant in its horrible way, the folks over at World of VS have taken up the challenge and created a proper Visual Studio extension that you should use. I'll chat with them and get some details and maybe a write-up of how they did it. So, while I encourage you to enjoy my tale below, go get the World of VS Default Browser Switcher now!

更新:为什么我自己的MacGyver解决方案以其可怕的方式出色,为什么VS World的人们已经接受了挑战,并创建了应使用的适当Visual Studio扩展。 我将与他们聊天,并获取一些详细信息,以及有关其工作方式的书面记录。 因此,尽管我鼓励您在下面欣赏我的故事,但现在就去获得VS Default Browser Switcher世界

I've heard and seen lots of complaints about how it's hard to set the default browser that Visual Studio launches when you launch a debug session for a website.

我听到并看到很多抱怨,当您为网站启动调试会话时,很难设置Visual Studio启动的默认浏览器。

步骤0-足够 (Step 0 - Adequate)

Folks spend time hunting around the Tools|Options dialog in Visual Studio looking for setting. They eventually realize it's not in there at all, but instead you have to right-click on an ASPX page within a Web Project and click "Browse With..."

人们花时间在Visual Studio的“工具|选项”对话框中寻找设置。 他们最终意识到它根本不存在,但是您必须在Web项目中的ASPX页面上单击鼠标右键,然后单击“浏览...”。

From this dialog you can click Set Default, which is totally obvious, right my daimies? Um, no. This doesn't work for ASP.NET MVC people who use other view engines and might not even have a .ASPX file in their solution. Plus, it's slow and irritating. Sa da tay.

在此对话框中,您可以单击“设置默认值”,这很明显,对吗? 不。 这对于使用其他视图引擎并且甚至可能在其解决方案中甚至没有.ASPX文件的ASP.NET MVC用户不起作用。 另外,它又慢又烦人。 萨达泰。

It IS interesting that I can add other browsers, like Google Chrome to this dialog via Add. Note that Google Chrome installs in C:\Users\Scott\appdata\Local\Google\Chrome\Application\chrome.exe which may not be c:\Program Files where you usually go hunting for these things.

我可以通过“添加”将其他浏览器(例如Google Chrome)添加到此对话框,这很有趣。 请注意,谷歌浏览器安装在C:\ Users \ Scott \ appdata \ Local \ Google \ Chrome \ Application \ chrome.exe中,该文件可能不是c:\ Program Files,通常您会在其中寻找这些东西。

“我的思想过程是什么”-或-“ CSI:Visual Studio默认浏览器” ("What my thought process was" - or - "CSI: Visual Studio Default Browser")

Where is this browser information stored? That was my first question. Remember that your computer is NOT a black box. Even good programmers make this mistake and they "flip this switch and hope that light turns on" without confirming that the switch and the light are connected with good wire and they know how electricity works.

该浏览器信息存储在哪里? 那是我的第一个问题。 请记住,您的计算机不是黑匣子。 即使是优秀的程序员犯这样的错误,他们“翻转这个开关和希望指示灯变为”不确认开关和光都具有良好的导线连接,他们知道电力是如何工作的。

I can guess all day, or I can open up ProcMon and just see for myself. Seriously, learn how to use this freaking tool. You can flip light switches all day or you can just open up the wall and see the wires. If you know how to use Process Monitor competently, people of both sexes will immediately find you more attractive. It's true. I get all sorts of free Tacos and Chips when folks look can I run ProcMon like Keanu Reeves can look sad.

我可以整天猜测,也可以打开ProcMon自己看看。 认真地学习如何使用此怪胎工具。 您可以整天打开电灯开关,也可以打开墙壁并查看电线。 如果您知道如何熟练使用Process Monitor,那么男女两性都会立即发现您更具吸引力。 这是真的。 当人们看起来我可以运行ProcMon时,如Keanu Reeves看起来很难过,我得到了各种各样的免费炸玉米饼和薯条。

I fired ProcMon up set it to only show the devenv.exe process, and I took a chance and set 'contains browser' for the path. If this didn't work I'd open the flood gates and start sifting a bit. I could also have said 'highlight' things with the word browser if I liked.

我解雇了ProcMon,将其设置为仅显示devenv.exe进程,然后我趁机为路径设置了“包含浏览器”。 如果这不起作用,我将打开水闸,然后开始过筛。 如果我喜欢的话,我也可以用浏览器一词说“突出”。

I launch VS, open the Browse With dialog and sweet sassy mollassy. Look at all that good stuff.

我启动VS,打开“浏览方式”对话框和甜美的混蛋。 看那些好东西。

Oh, fonts too small, let me zoom in.

哦,字体太小,让我放大。

Looks like we're reading values out of the registry at HKCU:\Software\Microsoft\VisualStudio\10.0\WebBrowser\ConfigTimestamp and...

看起来我们正在从HKCU的注册表中读取值:\ Software \ Microsoft \ VisualStudio \ 10.0 \ WebBrowser \ ConfigTimestamp和...

...reading out of the browsers.xml file at C:\Users\Scott\AppData\Local\Microsoft\VisualStudio\10.0\browsers.xml.

...读取C:\ Users \ Scott \ AppData \ Local \ Microsoft \ VisualStudio \ 10.0 \ browsers.xml上的browsers.xml文件。

What's in that file? I'm guessing XML with no schema, given it was probably 2003 when someone wrote this.

该文件中有什么? 我猜是没有模式的XML,因为有人写这个大概是2003年。

<?xml version="1.0"?><BrowserInfo>  <BrowserInfo>    <Browser>      <Name>Google Chrome</Name>      <Path>"C:\Users\Scott\AppData\Local\Google\Chrome\Application\chrome.exe"</Path>      <Resolution>0</Resolution>      <IsDefault>False</IsDefault>    </Browser>  </BrowserInfo></BrowserInfo>

I've seen folks attempt to change this with various extensions in Visual Studio and using automation calls within Visual Studio, but to the best of my knowledge, this feature has been in here for years and years and there's no way to get at it programmatically.

我已经看到人们试图通过Visual Studio中的各种扩展并在Visual Studio中使用自动化调用来更改此设置,但是据我所知,此功能已经存在了很多年,并且无法以编程方式获得它。 。

第1步-很棒 (Step 1 - Slightly Awesome)

Interestingly as well, my first attempt at changing the browser programmatically consisted of this brilliance:

同样有趣的是,我以编程方式更改浏览器的首次尝试包括以下方面:

C:\Users\Scott\AppData\Local\Microsoft\VisualStudio\10.0>copy "browsers - firefox.xml" browsers.xml /y        1 file(s) copied.

But, it seems that those registry keys are serious and really used for something, because each time I opened Browse With... I found my changes thrown away, probably because VS isn't watching for file for change notification, but rather caching the file in memory.

但是,这些注册表项似乎很重要并且确实用于某些用途,因为每次打开“浏览方式...”时,我都会发现我的更改被丢弃了,这可能是因为VS不在监视更改通知文件,而是将其缓存文件在内存中。

Looks like a job for PowerShell (yes, I know can do this with batch files, but PowerShell is way batter, so nyah. Learn it.)

看起来像是PowerShell的工作(是的,我知道可以使用批处理文件来做到这一点,但PowerShell连连击,所以,是的。了解它。)

First I need to figure out what's going on in this registry. If I got to that key (by right-clicking the key within ProcMon and clicking Jump To), then right-click on the key in the Registry Editor I get:

首先,我需要弄清楚此注册表中发生了什么。 如果我找到了该键(通过在ProcMon中右键单击该键并单击“跳转到”),然后在“注册表编辑器”中右键单击该键,则会得到:

Windows Registry Editor Version 5.00

[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\10.0\WebBrowser\ConfigTimestamp]"CacheFilePath"="C:\\Users\\Scott\\AppData\\Local\\Microsoft\\VisualStudio\\10.0\\browsers.xml""CacheFileSizeBytes"=dword:000000e6"CacheFileDateLastMod"=hex(b):6f,18,a4,ee,17,41,cb,01"LastConfigurationTimestamp"=hex(b):00,26,aa,86,09,41,cb,01

Ok, so that's the location of the file we already know about, and  looks like the file size in hex, as well as some magic goo for CacheFileDateLastMod and LastConfigurationTimestamp.

好的,这就是我们已经知道的文件的位置,看起来像十六进制的文件大小,以及CacheFileDateLastMod和LastConfigurationTimestamp的一些魔术。

One at a time. The CacheFileSizeBytes is easy. Did you know that the Windows 7 calculator was quietly upgraded while you were out there not learning how to convert hex in your head? It's true! (Yes, you can also just look at the decimal value in the Registry, but again, this is more fun. Yes, you could always convert between Hex and Dec in calc.exe, but again, fun. What's your beef? ;) )

一次一个。 CacheFileSizeBytes很简单。 您是否知道Windows 7计算器在学习如何转换十六进制的情况下悄悄升级了? 这是真的! (是的,您也可以只查看注册表中的十进制值,但是再次,这会更有趣。是的,您总是可以在calc.exe中在十六进制和Dec之间进行转换,但是,再次,有趣。您的牛肉是什么?;) )

Click Dec(imal) and looks like 230 bytes, the size of my browser.xml file. What's the deal with those mod dates, though? They a probably a Windows file time, if they aren't ticks. Ticks are seconds since 1970-1-1 and a Windows FileTime is the number of 100-nanosecond ticks since 1601-1-1 rounded to the nearest millisecond. Why? Because 1601 was an awesome year. I mean, the Battle of Kinsale stopped the siege in Kinsale, Ireland, and famine killed half the Estonians. Sheesh, that was a horrible year! What were we thinking!?

单击Dec(imal),它看起来像230个字节,即我的browser.xml文件的大小。 但是那些修改日期怎么办? 如果不打勾,则可能是Windows文件时间。 刻度是自1970-1-1以来的秒数,Windows FileTime是自1601-1-1舍入到最接近的毫秒以来的100纳秒的刻度数。 为什么? 因为1601是很棒的一年。 我的意思是,金塞尔战役停止了对爱尔兰金塞尔的包围,饥荒杀死了一半的爱沙尼亚人。 嘘,那是可怕的一年! 我们在想什么!?

Anyway, the easiest way to convert something you think might be a Date into a Date (and reason number 0x3b for you to finally learn PowerShell) is this line in PowerShell.

无论如何,将您认为可能是Date的内容转换为Date的最简单方法(以及最终要学习PowerShell的原因编号0x3b)是PowerShell中的这一行。

PS C:\> [DateTime]129268523480000000Saturday, August 21, 0410 8:19:08 AM

Lemme do that again against a blue background with a screenshot so you really believe me.

Lemme会在蓝色背景下使用屏幕截图再次执行该操作,因此您真的相信我。

Oh yes, I'm on a horse. Cool. Those are FileTimes. So, tappity tappity and here's a PowerShell script to check the current values and output them for testing.

哦,是的,我骑着马。 凉。 这些是FileTimes。 因此,tappity tappity和这是一个PowerShell脚本,用于检查当前值并将其输出以进行测试。

cd C:\Users\Scott\AppData\Local\Microsoft\VisualStudio\10.0

$browsers = [xml](get-content 'browsers.xml')$browsers.BrowserInfo.Browser.Name + " " + $browsers.BrowserInfo.Browser.Path

$regkey = "HKCU:\Software\Microsoft\VisualStudio\10.0\WebBrowser\ConfigTimestamp"

"LastConfigTimestamp:  " + ([DateTime](get-itemproperty $regkey).LastConfigurationTimestamp).ToLocalTime()"CacheFileDateLastMod: " + ([DateTime](get-itemproperty $regkey).CacheFileDateLastMod).ToLocalTime()"CacheFileSizeBytes:   " + (get-itemproperty $regkey).CacheFileSizeBytes

I'll go and develop and run this script in the PowerShell ISE (that's S for Scripting) that you already have on your computer. Freaky how Microsoft sneaks stuff like this on your machine. If you've got Windows 7, you're already got this.

我将在计算机上已经拥有的PowerShell ISE(用于脚本编写的S)中开发并运行此脚本 微软如何在您的计算机上偷偷摸摸地偷东西。 如果您有Windows 7,则已经拥有了。

I run Visual Studio and click Browse|With... a few times and watch the values change. Seems I need the SET to my GET script, so why not something like this. I've made copies of browsers.xml like browsers-chrome and browsers-firefox. You can do the same if you like.

我运行Visual Studio,然后单击Browse | With ...几次,然后观察值更改。 似乎我需要SET到我的GET脚本中,所以为什么不这样呢? 我已经制作了browsers.xml的副本,例如browsers-chrome和browsers-firefox。 如果愿意,您也可以这样做。

cd C:\Users\Scott\AppData\Local\Microsoft\VisualStudio\10.0 

copy '.\browsers - firefox.xml' .\browsers.xml$regkey = "HKCU:\Software\Microsoft\VisualStudio\10.0\WebBrowser\ConfigTimestamp" 

set-itemproperty $regkey -name LastConfigurationTimestamp -value (&{[DateTime]::Now.ToUniversalTime().ToFileTime()}) -type qwordset-itemproperty $regkey -name CacheFileDateLastMod -value (&{((dir .\browsers.xml).LastWriteTimeUtc).ToFileTime()}) -type qwordset-itemproperty $regkey -name CacheFileSizeBytes   -value (&{(dir .\browsers.xml).Length}) -type dword

What I am doing in this script? I'm copying my browser xml over the main one AND I'm updating the TimeStamp to now to get Visual Studio to re-read the file. Visual Studio seems to be checking and triple checking and if the CacheFileDateLastMod and CacheFileSizeBytes don't reflect reality, it will freak out and just delete my file completely and rebuild a new browsers.xml from scratch. Paranoid.

我在这个脚本中做什么? 我将浏览器xml复制到主要的XML上,并且将TimeStamp更新到现在,以使Visual Studio重新读取文件。 Visual Studio似乎正在检查和三重检查,并且如果CacheFileDateLastMod和CacheFileSizeBytes无法反映现实,它将吓坏了,只需完全删除我的文件并从头开始重新构建新的browsers.xml。 偏执狂。

You can go and fancy these scripts up with command-line parameters all you want because you're a better programmer than I, but I am all Save|As, baby. I have "UpdateDefaultBrowserToChrome.ps1" and, yes, wait for it, "UpdateDefaultBrowserToFireFox.ps1" and I sleep at night just fine, thank you very much.

您可以随心所欲地使用命令行参数来修饰这些脚本,因为您是比我更好的程序员,但是我全都是Save | As,宝贝。 我有“ UpdateDefaultBrowserToChrome.ps1”,是的,等待它,“ UpdateDefaultBrowserToFireFox.ps1”,我晚上睡得很好,非常感谢。

I can right click on them on my desktop and select Run with PowerShell if I like.

我可以在桌面上右键单击它们,然后根据需要选择“使用PowerShell运行”。

But...still....it could be more awesome. Darn my parents and the work ethic they instilled in me as a small child.

但是...仍然....可能会更棒。 我的父母和他们小时候灌输给我的职业道德。

第2步-非常好 (Step 2 - Extremely Awesome)

You can run PowerShell scripts from the regular not-really-DOS command line like this if you like.

您可以根据需要从常规的非真正DOS命令行运行PowerShell脚本。

C:\Users\Scott\Desktop>powershell .\UpdateDefaultVSBrowserToChrome.ps1

I could even install PowerConsole and run these commands from INSIDE Visual Studio 2010 if I like and I want to rip a hole in the space time continuum. My, is that intellisense inside PowerShell inside Visual Studio? Double sun power!!!!

如果愿意,我什至可以安装PowerConsole并从INSIDE Visual Studio 2010中运行这些命令,并且希望在时空连续体中破洞。 我的,是Visual Studio中PowerShell内部的智能感知吗? 双重太阳力量!

Still, this doesn't fit my mindless point and click mouse-like workflow. I'll go to Tools | External Tools and add two. Make sure you select the right PowerShell, which will be x86 even if you're on x64 so you correctly access the registry. I also checked Use Output window and added a single line of text at the bottom of each script.

但这仍然不适合我的盲点,并单击类似鼠标的工作流程。 我将转到“工具” | 外部工具并添加两个。 确保选择正确的PowerShell,即使您使用的是x64,它也将是x86,以便正确访问注册表。 我还检查了“使用输出”窗口,并在每个脚本的底部添加了一行文本。

Nice, but why not a toolbar?

不错,但是为什么不使用工具栏呢?

步骤3-获利! 我的意思是,真棒! (Step 3 - Profit! I mean, Profoundly Awesome!)

Right click on any Toolbar, this Customize and add buttons for External Tools (in my case #6 and #7) and...

右键单击任何工具栏,此“自定义并添加”外部工具(在我的情况下为#6和#7)和...的按钮

Which gives me this when I click:

当我单击时,这给了我:

And shows this in Browse With, showing me it worked:

并在“浏览方式”中显示了这一点,并向我展示了它的工作原理:

Yay! Enjoy.

好极了! 请享用。

汇总说明 (Roll Up Instructions)

  1. Go to C:\Users\YOURNAME\AppData\Local\Microsoft\VisualStudio\10.0 转到C:\ Users \ YOURNAME \ AppData \ Local \ Microsoft \ VisualStudio \ 10.0
  2. Make a bunch of browser-whatever xml files for your browsers, or populate your list from Visual Studio, then make copies. 制作一堆用于浏览器的浏览器-xml文件,或者从Visual Studio填充列表,然后进行复制。
  3. Make yourself a few of these (or make one that switches if you're awesome) 使自己成为其中的一些(或者,如果真棒的话,让其切换)
cd C:\Users\YOURNAME\AppData\Local\Microsoft\VisualStudio\10.0

copy '.\browsers - CUSTOMBROWSER.xml' .\browsers.xml$regkey = "HKCU:\Software\Microsoft\VisualStudio\10.0\WebBrowser\ConfigTimestamp"

set-itemproperty $regkey -name LastConfigurationTimestamp -value (&{[DateTime]::Now.ToUniversalTime().ToFileTime()}) -type qwordset-itemproperty $regkey -name CacheFileDateLastMod -value (&{((dir .\browsers.xml).LastWriteTimeUtc).ToFileTime()}) -type qwordset-itemproperty $regkey -name CacheFileSizeBytes   -value (&{(dir .\browsers.xml).Length}) -type dword"Bam, son. Browser changed to CUSTOM BROWSER."
  1. Add some external tools that call PowerShell with your new scripts as parameters. 添加一些使用新脚本作为参数调用PowerShell的外部工具。
  2. Add toolbar buttons if you feel like it. 如果需要,添加工具栏按钮。
  3. Go write a proper Visual Studio 2010 extension that does all this and packages it up in one click, put in on the VS Gallery and impress your friends and family. Crap. Now *I* need to do that for my next post, don't I? Shoot. Kzu? Give me a call and teach me how to do this.

    编写一个适当的Visual Studio 2010扩展程序,即可完成所有这些操作并将其打包,然后放入VS Gallery中并打动您的朋友和家人。 废话现在*我*需要在我的下一篇文章中这样做,不是吗? 射击。 zu ? 给我打个电话,教我如何做。

翻译自: https://www.hanselman.com/blog/how-to-change-the-default-browser-in-visual-studio-programmatically-with-powershell-and-possibly-poke-yourself-in-the-eye

powershell 编程

powershell 编程_如何使用PowerShell以编程方式更改Visual Studio中的默认浏览器,并可能使自己陷入困境...相关推荐

  1. 在 Visual Studio 中使用 Q# 进行量子编程

    1 量子计算机与量子编程 1.1 量子计算机 Quantum computing is computing using quantum-mechanical phenomena, such as su ...

  2. IDE之VS:利用 Visual Studio中的IDE配置python语言进行编程

    IDE之VS:利用 Visual Studio中的IDE配置python语言进行编程 目录 第一步,先安装python环境 第二步,加载本地已有的python 第一步,先安装python环境

  3. IDE之VS:利用 Visual Studio中的IDE配置C++语言进行编程

    IDE之VS:利用 Visual Studio中的IDE配置C++语言进行编程 目录 C++编译器之VS2015 1.新建项目,VisualC++,空项目,确定 2.右键项目文件夹,添加,新建 3.添 ...

  4. 深入理解python异步编程_深入理解Python异步编程

    1 什么是异步编程 1.1 阻塞程序未得到所需计算资源时被挂起的状态. 程序在等待某个操作完成期间,自身无法继续干别的事情,则称该程序在该操作上是阻塞的. 常见的阻塞形式有:网络I/O阻塞.磁盘I/O ...

  5. 高中生编程_高中生是否必须参加编程课程?

    高中生编程 孩子应该具备技术素养吗? 当然! 通过使他们成为程序员来教会他们这种技能的最好方法是吗? 好吧,这是一个更棘手的问题. 首先,直言不讳,在世界许多地方,即使在富裕地区,学校也没有能力处理这 ...

  6. python输入123输出321的编程_第2章 Python编程基础知识 第2.1节 简单的Python数据类型、变量赋值及输入输出...

    第三节 简单的Python数据类型.变量赋值及输入输出 Python是一门解释性语言,它的执行依赖于Python提供的执行环境,前面一章介绍了Python环境安装.WINDOWS系列Python编辑和 ...

  7. 【编程技巧】Code snippets(代码片段)在Visual Studio中的使用(附简单的Snippet管理工具)

    引言 Code snippet (代码片段)在VS中指的是基于IDE支持的利用快捷方式快速输入一小段,或者称之为一整块代码的功能,在日常编程,特别是在工作中写内容相似的业务代码时,利用Snippet功 ...

  8. sql azure 语法_方便SQL笔记本,用于在Azure Data Studio中进行故障排除

    sql azure 语法 This article prepares a handy SQL Notebook for DBAs. You can use this notebook to troub ...

  9. lisp pause 坐标值_lisp 已知坐标绘断面图_测量并写坐标(表格方式) - AutoLISP/Visual LISP...

    ;;;功能:测量并写坐标(表格方式) (输出XYZ) ;;;日期:zml84 于 2007-04-07 ;;;======================================== (def ...

最新文章

  1. Android分辨率适配layout布局的问题
  2. react native 中下拉列表FlatList组件的讲解以及实例demo
  3. Android软件开发需要学什么
  4. Java怎么不启动_dubbo不启动了怎么回事???一模一样的另一个没问题
  5. 两行命令实现 ubuntu 上自动更新 Vscode
  6. 20172329《程序设计与数据结构》实验一:线性结构实验报告
  7. Android提高显示布局文件的性能,使用include标签重用layouts
  8. Mybatis源码深度解析
  9. 各省农村人均受教育年限及村委会个数(2011-2019年)
  10. 腾讯笔试题 贪吃的小Q
  11. 畅销书排行榜html作业,制作畅销书排行榜.html
  12. rockchip 瑞芯微 SDK 一些解释
  13. YOLOv5 + Tesseract-OCR 实现车牌号文本识别
  14. Armeria 小试牛刀
  15. ikbc c87 Win键失灵/锁定
  16. 液晶显示模块(LCM)介绍
  17. [经验教程]手机上微信新消息不提示也不显示微信消息通知怎么办?
  18. Preference node org.eclipse.wst.validation 解决方法
  19. 超低功耗LoRa无线通信应用实践
  20. 笔记本安装Ubuntu 无法使用 Broadcom(博通) 无线网卡实现wifi上网的解决方法

热门文章

  1. Python 基础之linux基础相关
  2. Linux软件安装—软件包管理—rpm命令管理—包名与依赖性
  3. 2021全球与中国PCIe芯片市场现状及未来发展趋势
  4. mysql 1813_ERROR 1813 (HY000) at line 404: Tablespace for table '`xxx`.`xxxxx`' exists.
  5. 手机端扣扣浏览器图片居中_实现图片始终居中显示于浏览器窗口中心位置
  6. 联想MIIX 510-12ISK 改Win7系统
  7. Java之for循环打印倒直角三角形
  8. html5中如何让图片向下移动,如何调整HTML5的图像移动
  9. 学生成绩预测模型_逻辑回归实战练习——根据学生成绩预测是否被录取
  10. 自研机器人控制系统,挑战波士顿动力机器人