?? formaddusing.cs
字號(hào):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections.Specialized;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace ProgramCalculator
{
public partial class FormAddUsing : Form
{
private StringCollection referenceAssemblyCollection = new StringCollection();//所有dll
private StringCollection referenceNotSystemAssemblyCollection = new StringCollection();//非系統(tǒng)dll
/// <summary>
/// 獲取引用的程序集的完整路徑的集合
/// </summary>
public StringCollection ReferenceAssemblyCollection
{
get
{
return this.referenceAssemblyCollection;
}
}
public FormAddUsing()
{
InitializeComponent();
}
private void FormAddUsing_Load(object sender, EventArgs e)
{
//讀取.us文件中已有的using語句
string path = Function.GetPathOfUsingFile();
StreamReader sr = new StreamReader(path);
try
{
while (sr.Peek() != -1)
{
this.textBoxInput.Text += sr.ReadLine() + Environment.NewLine;
}
}
catch
{
}
finally
{
sr.Close();
}
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}
private void buttonOk_Click(object sender, EventArgs e)
{
this.referenceAssemblyCollection.AddRange(this.FindDllFormTextBox());
//修改編譯單元的引用程序集
CompilerUnit.ReferenceAssemblyCollection = this.referenceAssemblyCollection;
//將某些引用程序集拷貝到當(dāng)前目錄下
this.CopyReferenceAssemblyToStartupPath();
//保存.us文件
this.saveUsingFile();
this.DialogResult = DialogResult.OK;
}
/// <summary>
/// 根據(jù)文本查找其中的using語句,然后返回對(duì)應(yīng)的dll完整路徑集合
/// </summary>
/// <param name="txt"></param>
private string[] FindDllFormTextBox()
{
StringCollection sc = new StringCollection();
//-------------------
//根據(jù)using語句查找dll
//-----------------
foreach(string codeLine in this.textBoxInput.Lines)
{
string pattern = "(?<=using ).*(?=;)";
MatchCollection matchs = Regex.Matches(codeLine, pattern);
foreach (Match m in matchs)
{
string dll = m.Value + ".dll";
/*
* 檢查是否有對(duì)應(yīng)的.dll文件,就是看公共語言運(yùn)行庫的安裝目錄下有沒有對(duì)應(yīng)的
* .dll文件,下面是用絕對(duì)路徑檢查文件是否存在,這比讀取公共語言運(yùn)行庫的安裝目錄下所有
* 的.dll文件然后看是否有對(duì)應(yīng)名稱的dll要高效一些
*/
if (File.Exists(RuntimeEnvironment.GetRuntimeDirectory() + Path.DirectorySeparatorChar + dll) ||
File.Exists(RuntimeEnvironment.GetRuntimeDirectory() + Path.DirectorySeparatorChar +
"zh-CHS" + Path.DirectorySeparatorChar + dll)//不可靠的"zh_CHS";
)
{
sc.Add(dll);
}
}
}
string[] arr = new string[sc.Count];
sc.CopyTo(arr, 0);
return arr;
}
private void CopyReferenceAssemblyToStartupPath()
{
foreach (string sourceDll in this.referenceNotSystemAssemblyCollection)
{
try
{
string dest = Function.DirectoryOfFunctions + Path.DirectorySeparatorChar + Path.GetFileName(sourceDll);
File.Copy(sourceDll, dest, true);
}
catch { }
}
}
/// <summary>
/// 將using 語句保存到文件.注意這里不保證語句的正確性,如果編譯未通過請(qǐng)將該文件還原
/// </summary>
private void saveUsingFile()
{
StreamWriter sw = new StreamWriter(Function.GetPathOfUsingFile());
try
{
foreach (string line in this.textBoxInput.Lines)
{
sw.WriteLine(line + Environment.NewLine);
}
}
catch { }
finally
{
sw.Close();
}
}
private void buttonView_Click(object sender, EventArgs e)
{
this.openFileDialog1.InitialDirectory = RuntimeEnvironment.GetRuntimeDirectory();
this.openFileDialog1.Multiselect = false;
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
string path = this.openFileDialog1.FileName;
this.referenceAssemblyCollection.Add(path);
this.referenceNotSystemAssemblyCollection.Add(path);
//添加到listView
string[] arr = new string[2];
arr[0] = Path.GetFileName(path);
arr[1] = path;
ListViewItem lvi = new ListViewItem(arr);
this.listView1.Items.Insert(0, lvi);
}
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -