这个最近做一个程序时用到的一些知识点,特此记录想来以备后用!
1、测试代码执行时间的方法
Stopwatch sw = new Stopwatch();
sw.Start();
//这里填写要执行的代码
sw.Stop();
Console.WriteLine("总运行时间:" + sw.Elapsed);
Console.WriteLine("测量实例得出的总运行时间(毫秒为单位):" + sw.ElapsedMilliseconds);
Console.WriteLine("总运行时间(计时器刻度标识):" + sw.ElapsedTicks);
Console.WriteLine("计时器是否运行:" + sw.IsRunning.ToString());
2、文件路径及文件名字符串截取替换操作方法
string filePath = @"E:\Randy0528\中文目录\JustTest.rar";
Response.Write("文件路径:"+filePath);
Response.Write("<br/>更改路径字符串的扩展名。<br/>");
Response.Write(System.IO.Path.ChangeExtension(filePath, "txt"));
Response.Write("<br/>返回指定路径字符串的目录信息。。<br/>");
Response.Write(System.IO.Path.GetDirectoryName(filePath));
Response.Write("<br/>返回指定的路径字符串的扩展名。<br/>");
Response.Write(System.IO.Path.GetExtension(filePath));
Response.Write("<br/>返回指定路径字符串的文件名和扩展名。<br/>");
Response.Write(System.IO.Path.GetFileName(filePath));
Response.Write("<br/>返回不具有扩展名的指定路径字符串的文件名。<br/>");
Response.Write(System.IO.Path.GetFileNameWithoutExtension(filePath));
Response.Write("<br/>获取指定路径的根目录信息。<br/>");
Response.Write(System.IO.Path.GetPathRoot(filePath));
Response.Write("<br/>返回随机文件夹名或文件名。<br/>");
Response.Write(System.IO.Path.GetRandomFileName());
Response.Write("<br/>创建磁盘上唯一命名的零字节的临时文件并返回该文件的完整路径。<br/>");
Response.Write(System.IO.Path.GetTempFileName());
Response.Write("<br/>返回当前系统的临时文件夹的路径。<br/>");
Response.Write(System.IO.Path.GetTempPath());
Response.Write("<br/>确定路径是否包括文件扩展名。<br/>");
Response.Write(System.IO.Path.HasExtension(filePath));
Response.Write("<br/>获取一个值,该值指示指定的路径字符串是包含绝对路径信息还是包含相对路径信息。<br/>");
Response.Write(System.IO.Path.IsPathRooted(filePath));
3、System.IO.StreamWriter写文件换行
StreamWriter swPdfChange = new StreamWriter(txtfilename, false, Encoding.UTF8);
swPdfChange.Write("baPWV:" + str1 + " " + str2 + "\r\nABI:" + str3);
swPdfChange.Close();
StreamWriter swPdfChange = new StreamWriter(txtfilename, false, Encoding.UTF8);
swPdfChange.WriteLine("baPWV:" + str1 + " " + str2 );
swPdfChange.WriteLine("ABI:" + str3);
swPdfChange.Close();
4、追加文件字符到文本文件
StreamWriter wlog = File.AppendText(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase+"\\mylog.log");
wlog.WriteLine("出错文件:"+e.FullPath);
wlog.Flush();
wlog.Close();
5、控制台带参数程序
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _1
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("请输入参数 -a -v \"a s\" ");
}
else
{
foreach (string key in args)
{
if (key == "a s")
{
Console.WriteLine("This is ‘a s' parameters");
}
else if (key == "-a")
{
Console.WriteLine("This is ‘a' parameters");
}
else if (key == "-v")
{
Console.WriteLine("This is ‘v' parameters");
}
else
{
Console.WriteLine("参数错误");
}
}
}
}
}
}
6、隐藏窗口调用带参数控制台程序(另外一种思路是把控制台程序编译为窗口程序)
System.Diagnostics.Process p = new System.Diagnostics.Process();
p.StartInfo.FileName = currPath + "\\FcPdfGet.exe ";//需要启动的程序名
p.StartInfo.Arguments = e.FullPath;//启动参数
p.StartInfo.UseShellExecute = false;//不显示程序窗口
p.StartInfo.CreateNoWindow = true;
p.Start();//启动
System.Diagnostics.Process.Start(Application.StartupPath + "\\test.exe ", "参数1 参数2");
7、获取程序运行路径
//获取当前进程的完整路径,包含文件名(进程名)。
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取和设置包含该应用程序的目录的名称。(推荐)
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+"\")
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工作目录(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe文件所在的目录)
8、窗口程序退出弹确认对话框
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show("确定要退出!", "关闭窗口!", MessageBoxButtons.YesNo,MessageBoxIcon.Warning) == DialogResult.No)
{
e.Cancel = true;
}
}
9、实现窗口最小化到系统托盘
//1.设置WinForm窗体属性showinTask=false
//2.加notifyicon控件notifyIcon1,为控件notifyIcon1的属性Icon添加一个icon图标。
//3.添加窗体最小化事件(首先需要添加事件引用):
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
//上面一行是主窗体InitializeComponent()方法中需要添加的引用
private void Form1_SizeChanged(object sender, EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.notifyIcon1.Visible=true;
}
}
//4.添加点击图标事件(首先需要添加事件引用):
private void notifyIcon1_Click(object sender, EventArgs e)
{
this.Visible = true;
this.WindowState = FormWindowState.Normal;
this.notifyIcon1.Visible = false;
}
//5.可以给notifyIcon添加右键菜单:
//主窗体中拖入一个ContextMenu控件NicontextMenu,点中控件,在上下文菜单中添加菜单,notifyIcon1的ContextMenu行为中选中NicontextMenu 作为上下文菜单。
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.NicontextMenu = new System.Windows.Forms.ContextMenu();
this.menuItem_Hide = new System.Windows.Forms.MenuItem();
this.menuItem_Show = new System.Windows.Forms.MenuItem();
this.menuItem_Aubot = new System.Windows.Forms.MenuItem();
this.menuItem_Exit = new System.Windows.Forms.MenuItem();
this.notifyIcon1.ContextMenu = this.NicontextMenu;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject( "NotifyIcon.Icon ")));
this.notifyIcon1.Text = " ";
this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
this.notifyIcon1.Click += new System.EventHandler(this.notifyIcon1_Click);
this.NicontextMenu.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[]
{
this.menuItem_Hide,
this.menuItem_Show,
this.menuItem_Aubot,
this.menuItem_Exit
}
);
//
// menuItem_Hide
//
this.menuItem_Hide.Index = 0;
this.menuItem_Hide.Text = "隐藏 ";
this.menuItem_Hide.Click += new System.EventHandler(this.menuItem_Hide_Click);
//
// menuItem_Show
//
this.menuItem_Show.Index = 1;
this.menuItem_Show.Text = "显示 ";
this.menuItem_Show.Click += new System.EventHandler(this.menuItem_Show_Click);
//
// menuItem_Aubot
//
this.menuItem_Aubot.Index = 2;
this.menuItem_Aubot.Text = "关于 ";
this.menuItem_Aubot.Click += new System.EventHandler(this.menuItem_Aubot_Click);
//
// menuItem_Exit
//
this.menuItem_Exit.Index = 3;
this.menuItem_Exit.Text = "退出 ";
this.menuItem_Exit.Click += new System.EventHandler(this.menuItem_Exit_Click);
protected override void OnClosing(CancelEventArgs e)
{
this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
protected override void OnClosing(CancelEventArgs e)
{
//this.ShowInTaskbar = false;
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
private void CloseCtiServer()
{
timer.Enabled = false;
DJ160API.DisableCard();
this.NotifyIcon.Visible = false;
this.Close();
this.Dispose();
Application.Exit();
}
private void HideCtiServer()
{
this.Hide();
}
private void ShowCtiServer()
{
this.Show();
this.WindowState = FormWindowState.Normal;
this.Activate();
}
private void CtiManiForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
this.CloseCtiServer();
}
private void menuItem_Show_Click(object sender, System.EventArgs e)
{
this.ShowCtiServer();
}
private void menuItem_Aubot_Click(object sender, System.EventArgs e)
{
}
private void menuItem_Exit_Click(object sender, System.EventArgs e)
{
this.CloseCtiServer();
}
private void menuItem_Hide_Click(object sender, System.EventArgs e)
{
this.HideCtiServer();
}
private void CtiManiForm_SizeChanged(object sender, System.EventArgs e)
{
if(this.WindowState == FormWindowState.Minimized)
{
this.HideCtiServer();
}
}
private void notifyIcon1_DoubleClick(object sender,System.EventArgs e)
{
this.ShowCtiServer();
}
10、监控文件夹自动给图片文件打水印
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace FolderWatcher
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private static string text = "http://blog.youkuaiyun.com/wangqiuyun";
private static string path = @"E:\FolderWatcher";
private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.textBox1.Text))
{
path = this.textBox1.Text;
}
if (!string.IsNullOrEmpty(this.textBox2.Text))
{
text = this.textBox2.Text;
}
WatcherStrat(path, "*.*");
}
private static void WatcherStrat(string path, string filter)
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path;
watcher.Filter = filter;
watcher.Created += new FileSystemEventHandler(OnProcess);
watcher.EnableRaisingEvents = true;
watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess
| NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;
watcher.IncludeSubdirectories = true;
}
private static void OnProcess(object source, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created)
{
OnCreated(source, e);
}
}
private static void OnCreated(object source, FileSystemEventArgs e)
{
if (e.FullPath.IndexOf("_new.") < 0)
{
FinePic(e.FullPath, text, e.FullPath.Replace(".", "_new."), new Font("宋体", 15, FontStyle.Bold));
}
}
/// <summary>
/// 图片水印
/// </summary>
/// <param name="FileName">源文件路径</param>
/// <param name="wText">水印文字</param>
/// <param name="savePath">保存路径</param>
/// <param name="font">字体样式</param>
public static void FinePic(string FileName, string wText, string savePath, Font font)
{
Bitmap bmp = new Bitmap(FileName);
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.DrawString(wText, font, new SolidBrush(Color.FromArgb(70, Color.Red)), 60, bmp.Height - 120);//加水印
bmp.Save(savePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
以上资料来源于网上以及个人总结,特此声明!