公共函数收集
来源:互联网 发布:公历转农历算法 编辑:程序博客网 时间:2024/04/28 20:02
一些有用的公共函数
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
/// <summary>
/// 获取正在运行的实例,没有运行的实例返回null;
/// </summary>
public static Process RunningInstance()
{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
foreach (Process process in processes)
{
if (process.Id != current.Id)
{
if (Assembly.GetExecutingAssembly().Location.Replace("/", "\\") == current.MainModule.FileName)
{
return process;
}
}
}
return null;
}
/// <summary>
/// 显示已运行的程序。
/// </summary>
public static void HandleRunningInstance(Process instance)
{
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL); //显示,可以注释掉
SetForegroundWindow(instance.MainWindowHandle); //放到前端
}
public static bool PingIp(string ip)
{
string strIpOrDName = ip;
try
{
if (!IsIp(ip))
return false;
Ping objPingSender = new Ping();
PingOptions objPinOptions = new PingOptions();
objPinOptions.DontFragment = true;
string data = "";
byte[] buffer = Encoding.UTF8.GetBytes(data);
int intTimeout = 120;
PingReply objPinReply = objPingSender.Send(strIpOrDName, intTimeout, buffer, objPinOptions);
string strInfo = objPinReply.Status.ToString();
if (strInfo == "Success")
{
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
public static bool IsIp(string StrData)
{
Regex res = new Regex(@"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
return res.IsMatch(StrData);
}
/// <summary>
/// 把XML文档转换为Byte字节流
/// </summary>
/// <param name="XmlFilePath"></param>
/// <returns></returns>
public static byte[] XmlToByte(string XmlFilePath)
{
if (XmlFilePath == "" || XmlFilePath == null)
return null;
if (File.Exists(XmlFilePath) == false)
{
return null;
}
try
{
return File.ReadAllBytes(XmlFilePath);
}
catch
{
return null;
}
}
/// <summary>
/// 用来保存示教数据,如果读写数据库出现异常,可以手动导入示教数据而不用重新进行示教。
/// </summary>
/// <param name="trajname">路径名称作为示教文件名</param>
/// <param name="data">示教的字节流</param>
/// <returns></returns>
public static bool ByteToXml(string trajname, byte[] data)
{
if (data == null || trajname == null)
{
return false;
}
if (Directory.Exists(Application.StartupPath + "\\TeachDataFile") == false)
{
Directory.CreateDirectory(Application.StartupPath + "\\TeachDataFile");
}
try
{
MemoryStream l_stream = new MemoryStream(data);
l_stream.Position = 0;
StreamReader l_read = new StreamReader(l_stream);
XmlDocument l_doc = new XmlDocument();
l_doc.LoadXml(l_read.ReadToEnd());
l_doc.Save(Application.StartupPath + "\\TeachDataFile\\" + trajname + ".xml");
l_stream.Close();
l_read.Close();
return true;
}
catch
{
return false;
}
}
}
- 公共函数收集
- 常用公共函数收集
- C#公共类收集
- 函数收集
- delphi 公共函数
- Delphi公共函数(四)
- Delphi公共函数 (三)
- 常用公共函数
- JS的公共函数
- PHP常用公共函数
- 公共函数单元
- Yii使用公共函数
- 常用公共函数
- shell公共函数functions
- JavaScript公共验证函数
- ThinkPHP 公共函数整理
- laravel 公共函数
- map公共函数
- 数据库连接查询小结
- 乔布斯留给我们的人生启示
- Android 应用程序基础(Application Fundamentals)
- 远程调试
- object C 数据类型
- 公共函数收集
- listview长按事件 并获取选中行的值
- iphone学习笔记(五)
- 制作Linux U盘启动盘
- 导入css样式表的方法 _ link VS @import
- QT 模态对话框和非模态对话框
- symbian 不用资源构建密码框
- bonecp连接池遇到的问题
- S3C2440开发工具realview MDK4.22之库的使用