VS項目整體重命名工具
VS項目整體重命名工具
不再為項目重命名和修改命名空間而煩惱,簡單幾個字,但是開發加上測試大量項目,前前后后竟然跨越了1個月,汗。。。不過真正的開發時間可能2-3天的樣子。
一.介紹
1.雖然說我們平常不會經常出現項目重命名的情況,但是一旦出現,修改起來還是一項比較大的工程,并且還不一定修改完整。
2.當團隊發展到一定程度的時候,基本上都有了自己固定的一些WEB/Winform開發框架和通用項目模版,這樣就會出現修改項目名稱,命名空間等結構的情況。
3.噠噠噠噠噠噠,不說第三了。親,沒了。@_@
二.功能
1.自動重命名關聯的各種文件,并且支持自定義擴展。
2.自動檢測文件編碼格式,處理讀取不同編碼格式時出現的亂碼問題。(當項目內包含中文的時候)
3.自動修改sln和csproj文件中的引用關系,并且根據文件名稱修改文件夾名稱。
4.清理BIN目錄和OBJ目錄,已經項目產生的不必要的垃圾文件,可單獨使用。
5.每個csproj都是執行單獨修改,然后更新關聯引用,并記住修改過的引用。
6.輸入項目新名詞的文本框加入了選詞功能(文本框本身雙擊內容是全選的效果)。
7.自己試一下把。+_+
三.演示與截圖
1.VS的觀察
更新前的VS項目解決方案
更新后的VS項目解決方案
2.系統資源管理器的觀察
更新前的文件夾結構
更新后的文件夾結構
3.軟件使用截圖
1.打開軟件顯示很簡單的打開解決方案的視圖。
2.選擇解決方案打開后,會自動加載該目下所有的csproj文件。
3.點擊【軟件設置】會出現其他需要修改關聯的文件后綴名,你也可以直接寫文件全名。
你也可以給輸入新名稱的文本框添加選詞分隔符,以達到你所需要的自動選詞效果。
#p#
四.代碼和思路
1.文本框智能選詞功能的實現。
- public delegate void AutoSelectTextBoxDoubleclick(object sender, MouseEventArgs e);
- /// <summary>
- /// 智能選擇文本控件,親,這個類copy走就可以使用喲
- /// </summary>
- public class AutoSelectTextBox : TextBox
- {
- const int WM_LBUTTONDBLCLK = 0x0203;
- MouseEventArgs e;
- /// <summary>
- /// 是否啟用智能選擇
- /// </summary>
- [System.ComponentModel.Description("是否啟用智能選擇")]
- public bool EnableAutoSelect
- {
- get;
- set;
- }
- private List<char> splitChar;
- /// <summary>
- /// 選擇判斷分隔符,默認 . 號
- /// </summary>
- [System.ComponentModel.Description("判斷選擇分隔符,默認 . 號")]
- [System.ComponentModel.Browsable(false)]
- public List<char> SplitChar
- {
- get { return splitChar; }
- set { splitChar = value; }
- }
- /// <summary>
- /// 智能選擇文本框雙擊事件
- /// </summary>
- public event AutoSelectTextBoxDoubleclick AutoSelectTextMouseDoubleclick;
- protected override void WndProc(ref Message m)
- {
- if (EnableAutoSelect && m.Msg == WM_LBUTTONDBLCLK)
- {
- Point p = this.PointToClient(MousePosition);
- e = new MouseEventArgs(MouseButtons.Left, 2, p.X, p.Y, 0);
- if (AutoSelectTextMouseDoubleclick != null)
- {
- AutoSelectTextMouseDoubleclick(this, e);
- }
- else
- {
- MouseDoubleClickAutoSelect(e);
- }
- return;
- }
- else
- {
- base.WndProc(ref m);
- }
- }
- /// <summary>
- /// 智能選擇實現
- /// </summary>
- private void MouseDoubleClickAutoSelect(MouseEventArgs e)
- {
- if (this.Text != "")
- {
- int pint = this.GetCharIndexFromPosition(e.Location);
- int len = this.Text.Length;
- int left = pint, right = pint;
- while (left >= 0)
- {
- char lchar = this.Text[left];
- if (CheckSpiltChar(lchar))
- {
- break;
- }
- left--;
- }
- while (right <= len - 1)
- {
- char rchar = this.Text[right];
- if (CheckSpiltChar(rchar))
- {
- break;
- }
- right++;
- }
- //必須有字符可選
- if (right - (left + 1) > 0)
- {
- this.Select(left + 1, right - (left + 1));
- }
- }
- }
- /// <summary>
- /// 檢查
- /// </summary>
- /// <param name="source"></param>
- /// <returns></returns>
- private bool CheckSpiltChar(char source)
- {
- if (SplitChar != null)
- {
- foreach (char c in SplitChar)
- {
- if (char.Equals(source, c))
- {
- return true;
- }
- }
- return false;
- }
- else
- {
- return char.Equals(source, '.');
- }
- }
- }
解釋:該自動完成選詞功能需要重寫文本框的消息機制,其中 WM_LBUTTONDBLCLK = 0x0203 為鼠標雙擊消息,然后重寫WndProc該事件。
為什么我重新寫雙擊消息,而不是用文本框本身的雙擊事件,是因為本身的雙擊事件會處理一些其他內容,并且會出現先選中全部內容然后再自動選詞的閃爍問題,
有興趣的同學可以試一下就知道了。
2.老調重彈自動識別文件編碼問題,大部分的解決方案都不太理想,基本都是讀取文件2-4位,判斷字節大小,來決定文件編碼類型。
這樣的思路只是其中之一,當我郁悶至極的時候突然想到ITextSharp控件(重量級語言高亮編輯器),找到源碼,發現了一塊編碼自動識別的功能。
下面代碼是ITextSharp控件中文件編碼識別的源碼,這個我發現比百度和google出來的效果好很多,基本上無識別錯誤情況,基本100%識別正確。
但是帶來的后果是要檢查一段內容前面很大一段內容,用性能換編碼識別精度。
- public static StreamReader OpenStream(Stream fs, Encoding defaultEncoding)
- {
- if (fs == null)
- throw new ArgumentNullException("fs");
- if (fs.Length >= 2)
- {
- // the autodetection of StreamReader is not capable of detecting the difference
- // between ISO-8859-1 and UTF-8 without BOM.
- int firstByte = fs.ReadByte();
- int secondByte = fs.ReadByte();
- switch ((firstByte << 8) | secondByte)
- {
- case 0x0000: // either UTF-32 Big Endian or a binary file; use StreamReader
- case 0xfffe: // Unicode BOM (UTF-16 LE or UTF-32 LE)
- case 0xfeff: // UTF-16 BE BOM
- case 0xefbb: // start of UTF-8 BOM
- // StreamReader autodetection works
- fs.Position = 0;
- return new StreamReader(fs);
- default:
- return AutoDetect(fs, (byte)firstByte, (byte)secondByte, defaultEncoding);
- }
- }
- else
- {
- if (defaultEncoding != null)
- {
- return new StreamReader(fs, defaultEncoding);
- }
- else
- {
- return new StreamReader(fs);
- }
- }
- }
- static StreamReader AutoDetect(Stream fs, byte firstByte, byte secondByte, Encoding defaultEncoding)
- {
- int max = (int)Math.Min(fs.Length, 500000); // look at max. 500 KB
- const int ASCII = 0;
- const int Error = 1;
- const int UTF8 = 2;
- const int UTF8Sequence = 3;
- int state = ASCII;
- int sequenceLength = 0;
- byte b;
- for (int i = 0; i < max; i++)
- {
- if (i == 0)
- {
- b = firstByte;
- }
- else if (i == 1)
- {
- b = secondByte;
- }
- else
- {
- b = (byte)fs.ReadByte();
- }
- if (b < 0x80)
- {
- // normal ASCII character
- if (state == UTF8Sequence)
- {
- state = Error;
- break;
- }
- }
- else if (b < 0xc0)
- {
- // 10xxxxxx : continues UTF8 byte sequence
- if (state == UTF8Sequence)
- {
- --sequenceLength;
- if (sequenceLength < 0)
- {
- state = Error;
- break;
- }
- else if (sequenceLength == 0)
- {
- state = UTF8;
- }
- }
- else
- {
- state = Error;
- break;
- }
- }
- else if (b >= 0xc2 && b < 0xf5)
- {
- // beginning of byte sequence
- if (state == UTF8 || state == ASCII)
- {
- state = UTF8Sequence;
- if (b < 0xe0)
- {
- sequenceLength = 1; // one more byte following
- }
- else if (b < 0xf0)
- {
- sequenceLength = 2; // two more bytes following
- }
- else
- {
- sequenceLength = 3; // three more bytes following
- }
- }
- else
- {
- state = Error;
- break;
- }
- }
- else
- {
- // 0xc0, 0xc1, 0xf5 to 0xff are invalid in UTF-8 (see RFC 3629)
- state = Error;
- break;
- }
- }
- fs.Position = 0;
- switch (state)
- {
- case ASCII:
- case Error:
- // when the file seems to be ASCII or non-UTF8,
- // we read it using the user-specified encoding so it is saved again
- // using that encoding.
- if (IsUnicode(defaultEncoding))
- {
- // the file is not Unicode, so don't read it using Unicode even if the
- // user has choosen Unicode as the default encoding.
- // If we don't do this, SD will end up always adding a Byte Order Mark
- // to ASCII files.
- defaultEncoding = Encoding.Default; // use system encoding instead
- }
- return new StreamReader(fs, defaultEncoding);
- default:
- return new StreamReader(fs);
- }
- }
ITextSharp文件識別基本思路:
1.傳入需要讀取文件流,并且傳入一個你認為合適的編碼方式,當然你可以傳入NULL。
2.讀取文件前面2個字節(這個是大部分解析編碼需要做的事情),讀取進來的字節向左移8位,然后與第二個字節進行 | 運算,主要用途是檢測StreamReader是否在iso - 8859 - 1和utf - 8之間的編碼范圍,并且不包含BOM。
忘記了C#這些運算的可以看看,http://www.cnblogs.com/NatureSex/archive/2011/04/21/2023768.html
3.當位移出來的結果等于0x0000,0xfffe,0xfeff,0xefbb時,是StreamReader可以進行自動讀取范圍內的。如果不在范圍內時再進行讀取偏移檢測,具體怎么檢測的,這個有點難度,本人也只是看懂了一點點,主要是系統編碼知識欠缺。
五.下載
下載地址:VS重命名工具 喜歡的朋友記得推薦一下喲
源碼就不放了,文件沒有混淆也沒有加密,自己反編譯統統都有。
原文鏈接:http://www.cnblogs.com/NatureSex/archive/2013/03/21/2971596.html