首页 > 代码库 > C#语法功能结构
C#语法功能结构
1、File
打开指定文件夹或者文件,“\”为转义字符
System.Diagnostics.Process.Start(Application.StartupPath + "\\Document\\资源管理器\\");
复制文件到指定目录下
ToolStripItem Strip = sender as ToolStripItem;
string stripValue = http://www.mamicode.com/Strip.Text.ToString();
switch (stripValue)
{
case "自定义":
OpenFileDialog h_OpenFile = new OpenFileDialog();
h_OpenFile.Filter = "png文件|*.png";
h_OpenFile.InitialDirectory = "E:\\";
if (h_OpenFile.ShowDialog() == DialogResult.OK)
{
h_OpenFile.RestoreDirectory = true;
var pathBefore = h_OpenFile.FileName.Substring(h_OpenFile.FileName.LastIndexOf("\\") + 1);
var fileName = pathBefore.Substring(0, pathBefore.LastIndexOf("."));
contextMenuStrip1.Items.Add(fileName).Click += ToolStripMenuItem_Click;
FileInfo fileInfo = new FileInfo(h_OpenFile.FileName);
string appPath = Application.StartupPath + "\\Images\\Theme\\";
try
{
File.Copy(h_OpenFile.FileName, appPath + fileInfo.Name);
}
catch (Exception ex)
{
MessageBox.Show("文件已经存在!");
}
finally
{
h_OpenFile.Dispose();
}
}
break;
default:
try
{
this.BackgroundImage = Image.FromFile(Application.StartupPath + "\\Images\\Theme\\" + stripValue + ".png");
}
catch (Exception ex)
{
MessageBox.Show("背景不存在");
this.contextMenuStrip1.Items.Remove(Strip);
}
break;
}
读取指定路径下的文件夹并且遍历目录下的文件
string strPath = string.Format(Application.StartupPath + "\\Document\\资源管理器\\5、报表模板\\"); DirectoryInfo directoryName = new DirectoryInfo(strPath); foreach (FileInfo NextFile in directoryName.GetFiles()) { listViewEx1.Items.Add(NextFile.ToString(), 0); }
2、PictureBox
设置PictureBox背景图片为DeBug目录下的文件图片
pictureBox.BackgroundImage = Image.FromFile(Application.StartupPath + "\\Images\\弹窗类别\\" + "工作模版2.png");
3、ListView
获取listview选中项的首项
listView.FocusedItem.SubItems[0].Text;
鼠标拖拽实现
//窗体允许拖拽
this.AllowDrop = true;
//listview允许拖拽
this.listViewEx1.AllowDrop = true;
//订阅项拖拽事件
this.listViewEx1.ItemDrag += listViewEx1_ItemDrag;
//订阅拖入项事件
this.listViewEx1.DragEnter += listViewEx1_DragEnter;
4、String
"Jackie"+"Aillo"和String.Format("{0}{1}","Jackie", "Aillo")
5、Point
获取控件相对父元素的坐标
if (this.Width-20< e.X&&e.X<this.Width&&e.Y>panel2.Top&&e.Y<panel2.Top+panel2.Height) { panel2.Visible = true; } if (this.Height - 20 < e.Y && e.Y < this.Height&&e.X>this.Width-panel2.Width&&e.X<this.Width) { this.panel2.Visible = false; }
6、MessageBox
判断用户是否点击确定按钮
if(DialogResult.OK == MessageBox.Show("确定删除吗?", "提示", MessageBoxButtons.OKCancel))
{}
C#语法功能结构