今天在http://www.codeproject.com/useritems/Guidelines.as...上看到的一編文章,是個很好的工具,也可以作為如何操作註冊表的例子看一下。如下:
Introduction
介紹
When I logged on to CodeProject this morning, their weekly poll asked the question, "How wide is your sourcecode?". This led to a discussion in the forums, where Vikram posted a link to this article by Sara Ford:
我今天早上瀏覽CodeProject時,在周問題欄中有這樣一個問“你的程式碼片段有多長?”。這在論壇中引起一陣討論,其中Vikram提供了指向Sara Ford寫的一篇文章的連結:
http://blogs.msdn.com/saraford/archive/2004/11/15/257953.aspx
The article details a little hack that you can make to the registry so that one or more "guidelines" can be dispalyed in Visual Studio. So the bottom line is, this end result of this article isn't really due to my own bright idea - the only original part of this is in creating a tool to perform the registry hack for you with minimal effort on your part.
文章講述了一個小技巧:通過修改註冊表,可以在Visual Studio中顯示一或多條引導線。而我要講的是,我想到了一個更好的主意:做一個工具來完成註冊表的工作,而你只需使用就可以了。
I won't bother to rehash the content of the article here, as you can read it freely Sara's blog. I will simply address the tool, and what went into it.
我不會在這重複那篇文章的內容--你可以閱讀Sara的Blog(其實就是在註冊表的HKEY_CURRENT_USER/Software/Microsoft/VisualStudio/[Vertion(如8.0)]/Text Editor中加入一個索引值如("Guides",RGB(0,0,0) 4,80)就可以了--譯者注)。我只提供了工具的下載,並講一下它的原理。
The Tool
工具
The tool is so simple it's almost silly to review it. It took me about 10 minutes to hack together from start to finish. Basically, it allows you select a color to set the guidelines to, and to enter a list of column addresses to place the guidelines at. Click "Apply" and the changes are made. Click "Remove" and the changes are removed. It's a no brainer.
這個工具太簡單了,根本不值得去重新審視它。我一共只花了10分鐘就搞按下了。簡單地,它允許你設定引導線的顏色,以及引導線的位置。單擊“Apply”提交改變,單擊“Remove”刪去改變,根本不用動腦子。
The changes are made through some simple calls to the registry.
僅僅通過修改註冊表就可以實現改變。
private void btnApply_Click(object sender, EventArgs e){ if (txtPreview.Text != "" && txtLocations.Text != "") { // Create a new keyRegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\" +"VisualStudio\\8.0\\Text Editor", true); // Set value of sub key key.SetValue("Guides", txtPreview.Text);key.Close(); }}
private void btnRemove_Click(object sender, EventArgs e){RegistryKey delKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\" +"VisualStudio\\8.0\\Text Editor", true);delKey.DeleteValue("Guides");dekKey.Close();}
The IsNumeric Problem
是否是數位問題
C# is a wonderful language, but every now and then you wander into a situation that leaves you scratching your head in disbelief. One of the things I wanted to do was to make sure that the list of guideline locations was a comma seperated list of integers. My first thought (being an old VB programmer at heart) was to simply Split() the string and check to see if each value was a number by asking "IsNumeric()"? But lo and behold, C# has no such function! The Char type has a similar function, but not String.
C#是一個美妙的語言,但它有時也會讓你因懷疑而這抓耳撓腮,在解決方案中徘徊。一個例子是,我想確定那一串引導線位置參數是不是由逗號分隔的數字時,我的第一個想法是(我本身是一個老VB程式員),用Spit()提取分隔的每一頊,然後用IsNumeric()判斷是否為數字,可是你瞧,C#中沒有這個函數!Char類型的有一個類似的函數,而String沒有。
My first attempt (and you might have downloaded this copy, see the update below) was to Split() the string, and then "borrow" the function by stealing from VB, like this:
我的第一個嘗試是先Split()字串,然後從VB中借用IsNumeric():
private bool IsNumeric(string value){ return Microsoft.VisualBasic.Information.IsNumeric(value);}
That worked just fine for the most part, then I realized that you could still enter a value such a -9, or 9.5, and it would pass muster. While those values probably wouldn't hurt anything, they weren't helping it any either, so I came up with a new plan, and the one I should have gone with in the first place, using a regular expression to validate the input. Not only did this minimize a lot of code in my program, but it works better in the end.
大部分情況下它能很好地工作,但後來我意識到你仍然可以輸入-9、9.5這一類數,或者你可以傳一在堆。這些值不會讓程式報錯,但也沒有任何用處,所以我又有了一個新的計劃--對於第一部分,我應該用Regex來驗證輸入,它不但減少了我程式的代碼,而且工作得很好。
private bool verifyLocations(string locations){Regex regex = new Regex("^(\\d|,)*\\d*$");return regex.IsMatch(locations.Replace(" ", ""));}
原始碼下載