using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace IPADDemo.Util
{
public static class MyUtils
{
public static bool IsFileInUse(string fileName)
{
bool inUse = true;
FileStream fs = null;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
inUse = false;
}
catch (Exception ex)
{
try
{
Process[] pcs = Process.GetProcesses();
foreach (Process p in pcs)
{
if (p.MainModule.FileName == fileName)
{
p.Kill();
}
}
}
catch (Exception e) { }
}
finally
{
if (fs != null)
fs.Close();
}
return inUse;//true表示正在使用,false没有使用
}
public static Bitmap Base64StringToImage(string basestr)
{
Bitmap bitmap = null;
try
{
String inputStr = basestr;
byte[] arr = Convert.FromBase64String(inputStr);
MemoryStream ms = new MemoryStream(arr);
Bitmap bmp = new Bitmap(ms);
ms.Close();
bitmap = bmp;
//MessageBox.Show("转换成功!");
}
catch (Exception ex)
{
Console.WriteLine("Base64StringToImage 转换失败\nException:" + ex.Message);
}
return bitmap;
}
public static string ImageToBase64String(this Image image)
{
return Convert.ToBase64String(image.ImageToBytes());
}
///
/// 字符串转16进制
///
///
///
///
public static string StringToHexString(string s, Encoding encode)
{
byte[] b = encode.GetBytes(s);//按照指定编码将string编程字节数组
string result = string.Empty;
for (int i = 0; i < b.Length; i++)//逐字节变为16进制字符
{
result += Convert.ToString(b[i], 16);
}
return result;
}
///
/// 16进制转字符串
///
///
///
///
public static string HexStringToString(string hs, Encoding encode)
{
string strTemp = "";
byte[] b = new byte[hs.Length / 2];
for (int i = 0; i < hs.Length / 2; i++)
{
strTemp = hs.Substring(i * 2, 2);
b[i] = Convert.ToByte(strTemp, 16);
}
//按照指定编码将字节数组变为字符串
return encode.GetString(b);
}
}
}