// 获取程序的基目录。
System.AppDomain.CurrentDomain.BaseDirectory
// 获取模块的完整路径。
System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName
// 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。
System.Environment.CurrentDirectory
// 获取应用程序的当前工作目录。
System.IO.Directory.GetCurrentDirectory()
// 获取和设置包括该应用程序的目录的名称。
System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
// 获取启动了应用程序的可执行文件的路径。
System.Windows.Forms.Application.StartupPath
// 获取启动了应用程序的可执行文件的路径及文件名
System.Windows.Forms.Application.ExecutablePath
///TreeView checkbox实现单选。。
private void treeView1_BeforeCheck(object sender, TreeViewCancelEventArgs e)
{
if (e.Action != TreeViewAction.Unknown)
{
TreeNode oldNode = this.treeView1.Tag as TreeNode;
if (oldNode != null && oldNode != e.Node)
{
oldNode.Checked = false;
}
TreeNodeCollection nodes = this.treeView1.Nodes;
if (e.Node.Parent != null)
{
nodes = e.Node.Parent.Nodes;
}
foreach (TreeNode node in nodes)
{
if (node != e.Node)
{
node.Checked = false;
}
}
this.treeView1.Tag = e.Node;
}
}
3.窗体的TopMast属性可以使打开的窗口始终置于桌面最前端
4.richTextBox1使滚动条致最低端
//选择richtextbox中内容的最后一个字节
this.richTextBox1.Select(this.richTextBox1.Text.Length, 0);
//设置滚动到当前位置
this.richTextBox1.ScrollToCaret();
5. //算字符的所占像素长度
Font arialBold = new Font("SimSun", 9.0F);
Size textSize = TextRenderer.MeasureText(StringStr, arialBold);
6.划线
pnl.Paint += new PaintEventHandler(pnl_Paint);
void pnl_Paint(object sender, PaintEventArgs e)
{
Graphics gLine = e.Graphics;
gLine.DrawLine(Pens.LightGray, new Point(20, 2), new Point(960, 2)); //画横线,Y坐标相同
gLine.DrawLine(Pens.LightGray, new Point(20, 20), new Point(20,50)); //画竖线,X坐标相同
}
7.获得外网IP
public static string GetIP()
{
Uri uri = new Uri("http://city.ip138.com/ip2city.asp");
System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Method = "get";
using (Stream s = req.GetResponse().GetResponseStream())
{
using (StreamReader reader = new StreamReader(s))
{
char[] ch = {'[',']' };
string str = reader.ReadToEnd();
System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(str, @"\[(?<IP>[0-9\.]*)\]");
return m.Value.Trim(ch) ;
}
}
}
// 拦截双击标题栏的系统消息,防止双击使其最小化
protected override void WndProc(ref Message m)
{
if (m.Msg != 0xA3)
base.WndProc(ref m);
}
提取页面的href和链接文本
MatchCollection mc = Regex.Matches(href2,@"<a\s?href=(?<url>.*?)>(?<content>.*?)</a>");
string s = "";
foreach (Match m in mc)
{
s += m.Groups["content"].Value+"\n";
}
MessageBox.Show(s);
public bool IsCheck(string Str)
{
return System.Text.RegularExpressions.Regex.IsMatch(str, @"^(\d{3,4}-)?\d{6,8}$");
}
listBox设置选项颜色
加载时设置this.listBox1.DrawMode= System.Windows.Forms.DrawMode.OwnerDrawFixed;
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string s = this.listBox1.Items[e.Index].ToString();
if (s.Contains("初始化成功"))
{
e.Graphics.DrawString(s, this.Font, Brushes.Green, e.Bounds);
}
else if (s.Contains("初始化失败"))
{
e.Graphics.DrawString(s, this.Font, Brushes.Red, e.Bounds);
}
else
e.Graphics.DrawString(s, this.Font, new SolidBrush(this.ForeColor), e.Bounds);
}
//得到控件
for(int i=0;i<10;i++)
{
object o = this.GetType().GetField("lblRealPrice"+i, System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(this);
}
Xml绑定TreeView
private XmlDocument xml = new XmlDocument();
private void Form4_Load(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory + @"NameList.xml";
xml.Load(path);//加载xml文件
bindTvXml();
}
/// <summary>
/// 绑定TreeView
/// </summary>
private void bindTvXml()
{
for (int i = 0; i < xml.DocumentElement.ChildNodes.Count; i++)
{
XmlNode Xnode = xml.DocumentElement.ChildNodes[i];
TreeNode node = new TreeNode();
node.Text = Xnode.Attributes["Name"].Value;
node.Tag = Xnode;
node.ImageIndex = 0;
bindChildNode(node, Xnode);//绑定子节点
TvXml.Nodes.Add(node);
TvXml.HideSelection = false;
}
}
/// <summary>
/// 递归绑定子节点
/// </summary>
/// <param name="node"></param>
/// <param name="xml"></param>
private void bindChildNode(TreeNode node, XmlNode xml)
{
for (int i = 0; i < xml.ChildNodes.Count; i++)
{
TreeNode Childnode = new TreeNode();
XmlNode ChildXml = xml.ChildNodes[i];
Childnode.Text = ChildXml.Value;
Childnode.Name = "1";
Childnode.Tag = xml.ChildNodes[i];
if (ChildXml.HasChildNodes)
{
if (ChildXml.ChildNodes[0].NodeType == XmlNodeType.Text)
Childnode.Text = ChildXml.ChildNodes[0].InnerText;
else
bindChildNode(Childnode, ChildXml);
Childnode.ImageIndex = 1;
}
node.Nodes.Add(Childnode);
}
}