using System;
using System.Security.Cryptography;
namespace ArLi.CommonPrj {
public sealed class RandomStr {
/********
* Const and Function
* ********/
private static readonly int defaultLength = 8;
private static int GetNewSeed(){
byte[] rndBytes = new byte[4];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(rndBytes);
return BitConverter.ToInt32(rndBytes,0);
}
/********
* getRndCode of all char .
* ********/
private static string BuildRndCodeAll(int strLen) {
System.Random RandomObj = new System.Random(GetNewSeed());
string buildRndCodeReturn = null;
for(int i=0; i<strLen; i++) {
buildRndCodeReturn += (char)RandomObj.Next(33,125);
}
return buildRndCodeReturn;
}
public static string GetRndStrOfAll() {
return BuildRndCodeAll(defaultLength);
}
public static string GetRndStrOfAll(int LenOf) {
return BuildRndCodeAll(LenOf);
}
/********
* getRndCode of only .
* ********/
private static string sCharLow = "abcdefghijklmnopqrstuvwxyz";
private static string sCharUpp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static string sNumber = "0123456789";
private static string BuildRndCodeOnly(string StrOf,int strLen) {
System.Random RandomObj = new System.Random(GetNewSeed());
string buildRndCodeReturn = null;
for(int i=0; i<strLen; i++) {
buildRndCodeReturn += StrOf.Substring(RandomObj.Next(0,StrOf.Length-1),1);
}
return buildRndCodeReturn;
}
public static string GetRndStrOnlyFor() {
return BuildRndCodeOnly(sCharLow + sNumber,defaultLength);
}
public static string GetRndStrOnlyFor(int LenOf) {
return BuildRndCodeOnly(sCharLow + sNumber,LenOf);
}
public static string GetRndStrOnlyFor(bool bUseUpper,bool bUseNumber) {
string strTmp = sCharLow;
if (bUseUpper) strTmp += sCharUpp;
if (bUseNumber) strTmp += sNumber;
return BuildRndCodeOnly(strTmp,defaultLength);
}
public static string GetRndStrOnlyFor(int LenOf,bool bUseUpper,bool bUseNumber) {
string strTmp = sCharLow;
if (bUseUpper) strTmp += sCharUpp;
if (bUseNumber) strTmp += sNumber;
return BuildRndCodeOnly(strTmp,LenOf);
}
}
}
文件夹选择对话框
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
namespace ArLi.CommonPrj {
#region how use this?
/*
FolderBrowser fbObj = new FolderBrowser();
fbObj.Title = "Select a Folder";
fbObj.Flags =
BrowseFlags.BIF_NEWDIALOGSTYLE|
BrowseFlags.BIF_EDITBOX|
BrowseFlags.BIF_STATUSTEXT
;
DialogResult result = fbObj.ShowFolderBrowser();
if (result == DialogResult.OK ) {
MessageBox.Show(fbObj.DirectoryPath);
}
*/
#endregion
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
[ComVisible(true)]
public class BROWSEINFO {
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}
[Flags, Serializable]
public enum BrowseFlags {
BIF_DEFAULT = 0x0000,
BIF_BROWSEFORCOMPUTER = 0x1000,
BIF_BROWSEFORPRINTER = 0x2000,
BIF_BROWSEINCLUDEFILES = 0x4000,
BIF_BROWSEINCLUDEURLS = 0x0080,
BIF_DONTGOBELOWDOMAIN = 0x0002,
BIF_EDITBOX = 0x0010,
BIF_NEWDIALOGSTYLE = 0x0040,
BIF_NONEWFOLDERBUTTON = 0x0200,
BIF_RETURNFSANCESTORS = 0x0008,
BIF_RETURNONLYFSDIRS = 0x0001,
BIF_SHAREABLE = 0x8000,
BIF_STATUSTEXT = 0x0004,
BIF_UAHINT = 0x0100,
BIF_VALIDATE = 0x0020,
BIF_NOTRANSLATETARGETS = 0x0400,
}
public class API {
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath);
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);
}
public class FolderBrowser {
private string m_strDirectoryPath;
private string m_strTitle;
private string m_strDisplayName;
private BrowseFlags m_Flags;
public FolderBrowser() {
m_Flags = BrowseFlags.BIF_DEFAULT;
m_strTitle = "";
}
public string DirectoryPath {
get{return this.m_strDirectoryPath;}
}
public string DisplayName {
get{return this.m_strDisplayName;}
}
public string Title {
set{this.m_strTitle = value;}
}
public BrowseFlags Flags {
set{this.m_Flags = value;}
}
public DialogResult ShowFolderBrowser() {
BROWSEINFO bi = new BROWSEINFO();
bi.pszDisplayName = IntPtr.Zero;
bi.lpfn = IntPtr.Zero;
bi.lParam = IntPtr.Zero;
bi.lpszTitle = "Select Folder";
IntPtr idListPtr = IntPtr.Zero;
IntPtr pszPath = IntPtr.Zero;
try {
if (this.m_strTitle.Length != 0) {
bi.lpszTitle = this.m_strTitle;
}
bi.ulFlags = (int)this.m_Flags;
bi.pszDisplayName = Marshal.AllocHGlobal(256);
idListPtr = API.SHBrowseForFolder(bi);
if (idListPtr == IntPtr.Zero) {
return DialogResult.Cancel;
}
pszPath = Marshal.AllocHGlobal(256);
bool bRet = API.SHGetPathFromIDList(idListPtr, pszPath);
m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath);
this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
}
catch (Exception ex) {
Trace.WriteLine(ex.Message);
return DialogResult.Abort;
}
finally {
if (idListPtr != IntPtr.Zero) {
Marshal.FreeHGlobal(idListPtr);
}
if (pszPath != IntPtr.Zero) {
Marshal.FreeHGlobal(pszPath);
}
if (bi != null) {
Marshal.FreeHGlobal(bi.pszDisplayName);
}
}
return DialogResult.OK;
}
}
}
获取资源文件内容
using System;
using System.Reflection;
using System.Resources;
namespace ArLi.CommonPrj {
internal class getResPrj {
internal static object GetResOf(string resFullName,string resItemName) {
Assembly myAssem = Assembly.GetEntryAssembly();
ResourceManager rm = new ResourceManager(resFullName,myAssem);
return rm.GetObject(resItemName);
}
}
}
获取硬盘序列号
using System;
using System.Runtime.InteropServices;
namespace ArLi.CommonPrj {
#region how use this?
/*
string sVol = GetVolOf("C");
*/
#endregion
public class getvol{
[DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);
public static string GetVolOf(string drvID){
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int a =0;
int b =0;
string str1 = null;
string str2 = null;
int i = GetVolumeInformation(
drvID + @":/",
str1,
MAX_FILENAME_LEN,
ref retVal,
a,
b,
str2,
MAX_FILENAME_LEN
);
return retVal.ToString("x");
}
}
}
禁止程序重复运行限制
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
namespace ArLi.CommonPrj
{
public class one_instance_Check
{
public one_instance_Check()
{
}
#region how use this?
/*
using ArLi.CommonPrj;
if (one_instance_Check.goCheck("Process Exist !")) {
Application.Run (new Form1());
}
*/
#endregion
[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;
public static bool GoCheck(string waringMessage_ifExist) {
Process instance = RunningInstance();
if (instance == null) {
return true;
}
else {
if (waringMessage_ifExist != null) {
System.Windows.Forms.MessageBox.Show(
null,
waringMessage_ifExist,
System.Windows.Forms.Application.ProductName.ToString(),
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation
);
}
HandleRunningInstance(instance);
return false;
}
}
private static Process RunningInstance() {
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes) {
//Ignore the current process
if (process.Id != current.Id) {
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") ==
current.MainModule.FileName) {
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
private static void HandleRunningInstance(Process instance) {
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
}
using System.Security.Cryptography;
namespace ArLi.CommonPrj {
public sealed class RandomStr {
/********
* Const and Function
* ********/
private static readonly int defaultLength = 8;
private static int GetNewSeed(){
byte[] rndBytes = new byte[4];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(rndBytes);
return BitConverter.ToInt32(rndBytes,0);
}
/********
* getRndCode of all char .
* ********/
private static string BuildRndCodeAll(int strLen) {
System.Random RandomObj = new System.Random(GetNewSeed());
string buildRndCodeReturn = null;
for(int i=0; i<strLen; i++) {
buildRndCodeReturn += (char)RandomObj.Next(33,125);
}
return buildRndCodeReturn;
}
public static string GetRndStrOfAll() {
return BuildRndCodeAll(defaultLength);
}
public static string GetRndStrOfAll(int LenOf) {
return BuildRndCodeAll(LenOf);
}
/********
* getRndCode of only .
* ********/
private static string sCharLow = "abcdefghijklmnopqrstuvwxyz";
private static string sCharUpp = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private static string sNumber = "0123456789";
private static string BuildRndCodeOnly(string StrOf,int strLen) {
System.Random RandomObj = new System.Random(GetNewSeed());
string buildRndCodeReturn = null;
for(int i=0; i<strLen; i++) {
buildRndCodeReturn += StrOf.Substring(RandomObj.Next(0,StrOf.Length-1),1);
}
return buildRndCodeReturn;
}
public static string GetRndStrOnlyFor() {
return BuildRndCodeOnly(sCharLow + sNumber,defaultLength);
}
public static string GetRndStrOnlyFor(int LenOf) {
return BuildRndCodeOnly(sCharLow + sNumber,LenOf);
}
public static string GetRndStrOnlyFor(bool bUseUpper,bool bUseNumber) {
string strTmp = sCharLow;
if (bUseUpper) strTmp += sCharUpp;
if (bUseNumber) strTmp += sNumber;
return BuildRndCodeOnly(strTmp,defaultLength);
}
public static string GetRndStrOnlyFor(int LenOf,bool bUseUpper,bool bUseNumber) {
string strTmp = sCharLow;
if (bUseUpper) strTmp += sCharUpp;
if (bUseNumber) strTmp += sNumber;
return BuildRndCodeOnly(strTmp,LenOf);
}
}
}
文件夹选择对话框
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Collections;
namespace ArLi.CommonPrj {
#region how use this?
/*
FolderBrowser fbObj = new FolderBrowser();
fbObj.Title = "Select a Folder";
fbObj.Flags =
BrowseFlags.BIF_NEWDIALOGSTYLE|
BrowseFlags.BIF_EDITBOX|
BrowseFlags.BIF_STATUSTEXT
;
DialogResult result = fbObj.ShowFolderBrowser();
if (result == DialogResult.OK ) {
MessageBox.Show(fbObj.DirectoryPath);
}
*/
#endregion
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
[ComVisible(true)]
public class BROWSEINFO {
public IntPtr hwndOwner;
public IntPtr pidlRoot;
public IntPtr pszDisplayName;
public string lpszTitle;
public int ulFlags;
public IntPtr lpfn;
public IntPtr lParam;
public int iImage;
}
[Flags, Serializable]
public enum BrowseFlags {
BIF_DEFAULT = 0x0000,
BIF_BROWSEFORCOMPUTER = 0x1000,
BIF_BROWSEFORPRINTER = 0x2000,
BIF_BROWSEINCLUDEFILES = 0x4000,
BIF_BROWSEINCLUDEURLS = 0x0080,
BIF_DONTGOBELOWDOMAIN = 0x0002,
BIF_EDITBOX = 0x0010,
BIF_NEWDIALOGSTYLE = 0x0040,
BIF_NONEWFOLDERBUTTON = 0x0200,
BIF_RETURNFSANCESTORS = 0x0008,
BIF_RETURNONLYFSDIRS = 0x0001,
BIF_SHAREABLE = 0x8000,
BIF_STATUSTEXT = 0x0004,
BIF_UAHINT = 0x0100,
BIF_VALIDATE = 0x0020,
BIF_NOTRANSLATETARGETS = 0x0400,
}
public class API {
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern IntPtr SHBrowseForFolder(BROWSEINFO bi);
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern bool SHGetPathFromIDList(IntPtr pidl, IntPtr pszPath);
[DllImport("shell32.dll", PreserveSig=true, CharSet=CharSet.Auto)]
public static extern int SHGetSpecialFolderLocation(IntPtr hwnd, int csidl, ref IntPtr ppidl);
}
public class FolderBrowser {
private string m_strDirectoryPath;
private string m_strTitle;
private string m_strDisplayName;
private BrowseFlags m_Flags;
public FolderBrowser() {
m_Flags = BrowseFlags.BIF_DEFAULT;
m_strTitle = "";
}
public string DirectoryPath {
get{return this.m_strDirectoryPath;}
}
public string DisplayName {
get{return this.m_strDisplayName;}
}
public string Title {
set{this.m_strTitle = value;}
}
public BrowseFlags Flags {
set{this.m_Flags = value;}
}
public DialogResult ShowFolderBrowser() {
BROWSEINFO bi = new BROWSEINFO();
bi.pszDisplayName = IntPtr.Zero;
bi.lpfn = IntPtr.Zero;
bi.lParam = IntPtr.Zero;
bi.lpszTitle = "Select Folder";
IntPtr idListPtr = IntPtr.Zero;
IntPtr pszPath = IntPtr.Zero;
try {
if (this.m_strTitle.Length != 0) {
bi.lpszTitle = this.m_strTitle;
}
bi.ulFlags = (int)this.m_Flags;
bi.pszDisplayName = Marshal.AllocHGlobal(256);
idListPtr = API.SHBrowseForFolder(bi);
if (idListPtr == IntPtr.Zero) {
return DialogResult.Cancel;
}
pszPath = Marshal.AllocHGlobal(256);
bool bRet = API.SHGetPathFromIDList(idListPtr, pszPath);
m_strDirectoryPath = Marshal.PtrToStringAuto(pszPath);
this.m_strDisplayName = Marshal.PtrToStringAuto(bi.pszDisplayName);
}
catch (Exception ex) {
Trace.WriteLine(ex.Message);
return DialogResult.Abort;
}
finally {
if (idListPtr != IntPtr.Zero) {
Marshal.FreeHGlobal(idListPtr);
}
if (pszPath != IntPtr.Zero) {
Marshal.FreeHGlobal(pszPath);
}
if (bi != null) {
Marshal.FreeHGlobal(bi.pszDisplayName);
}
}
return DialogResult.OK;
}
}
}
获取资源文件内容
using System;
using System.Reflection;
using System.Resources;
namespace ArLi.CommonPrj {
internal class getResPrj {
internal static object GetResOf(string resFullName,string resItemName) {
Assembly myAssem = Assembly.GetEntryAssembly();
ResourceManager rm = new ResourceManager(resFullName,myAssem);
return rm.GetObject(resItemName);
}
}
}
获取硬盘序列号
using System;
using System.Runtime.InteropServices;
namespace ArLi.CommonPrj {
#region how use this?
/*
string sVol = GetVolOf("C");
*/
#endregion
public class getvol{
[DllImport("kernel32.dll")]
private static extern int GetVolumeInformation(
string lpRootPathName,
string lpVolumeNameBuffer,
int nVolumeNameSize,
ref int lpVolumeSerialNumber,
int lpMaximumComponentLength,
int lpFileSystemFlags,
string lpFileSystemNameBuffer,
int nFileSystemNameSize
);
public static string GetVolOf(string drvID){
const int MAX_FILENAME_LEN = 256;
int retVal = 0;
int a =0;
int b =0;
string str1 = null;
string str2 = null;
int i = GetVolumeInformation(
drvID + @":/",
str1,
MAX_FILENAME_LEN,
ref retVal,
a,
b,
str2,
MAX_FILENAME_LEN
);
return retVal.ToString("x");
}
}
}
禁止程序重复运行限制
using System;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Reflection;
namespace ArLi.CommonPrj
{
public class one_instance_Check
{
public one_instance_Check()
{
}
#region how use this?
/*
using ArLi.CommonPrj;
if (one_instance_Check.goCheck("Process Exist !")) {
Application.Run (new Form1());
}
*/
#endregion
[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;
public static bool GoCheck(string waringMessage_ifExist) {
Process instance = RunningInstance();
if (instance == null) {
return true;
}
else {
if (waringMessage_ifExist != null) {
System.Windows.Forms.MessageBox.Show(
null,
waringMessage_ifExist,
System.Windows.Forms.Application.ProductName.ToString(),
System.Windows.Forms.MessageBoxButtons.OK,
System.Windows.Forms.MessageBoxIcon.Exclamation
);
}
HandleRunningInstance(instance);
return false;
}
}
private static Process RunningInstance() {
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName (current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes) {
//Ignore the current process
if (process.Id != current.Id) {
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "//") ==
current.MainModule.FileName) {
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
private static void HandleRunningInstance(Process instance) {
//Make sure the window is not minimized or maximized
ShowWindowAsync (instance.MainWindowHandle , WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow (instance.MainWindowHandle);
}
}
}