Asp.NET Tutorials
Home > C#语言 > c#常用代码

Hot archives

c#常用代码
来源:csdn
c#常用代码  
----------------------------------------------------------------------

随机密码

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;
using  System.IO;


namespace  ArLi.CommonPrj  {
internal  class  FileOp  {
internal  static  string  ReadFileOf(string  fileFullName)  {
string  fileBody  =  "";
try{
FileStream  ObjFile  =  new  FileStream(fileFullName,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
StreamReader  sw  =  new  StreamReader(ObjFile,System.Text.Encoding.Default);
fileBody  =  sw.ReadToEnd();
sw.Close();
ObjFile.Close();
}catch  (Exception  e){
fileBody  =  e.Message.ToString();
}
return  fileBody;
}
internal  static  string  SaveFileOf(string  fileFullName,string  fileBody)  {
try{
FileStream  ObjFile  =  new  FileStream(fileFullName,FileMode.Create,FileAccess.Write,FileShare.Read);
StreamWriter  sw  =  new  StreamWriter(ObjFile,System.Text.Encoding.Default);
sw.Write(fileBody);
sw.Close();
ObjFile.Close();
return  null;
}catch  (Exception  e){
return  e.Message.ToString();
}
}
}
}


播放声音

using  system;
using  System.Runtime.InteropServices;

namespace  arli.commonprj  {
internal  class  sndPlay  {

[dllimport("winmm.dll",  entrypoint="playsound")]
internal  static  extern  bool  PlaySound(ref  Byte  snd,  IntPtr  hmod,  uint  fdwSound);

#region  how  use  this  ?
/*  

//get  wav  byte[]  from  resourcefile  to  sound
ArLi.CommonPrj.sndPlay.sound=(System.Byte[])ArLi.CommonPrj.getResPrj.getResOf("namespace.ResourceFileName","ResourceItemName");
//play  of:
ArLi.CommonPrj.sndPlay.PlaySound(ref  ArLi.CommonPrj.sndPlay.sound[0],IntPtr.Zero,(uint)ArLi.CommonPrj.sndPlay.PlayingFlags.SND_MEMORY  |  (uint)ArLi.CommonPrj.sndPlay.PlayingFlags.SND_ASYNC);
//stop  of;
ArLi.CommonPrj.sndPlay.sound  =  new  System.Byte[]{0};
ArLi.CommonPrj.sndPlay.PlaySound(ref  ArLi.CommonPrj.sndPlay.sound[0],IntPtr.Zero,(uint)ArLi.CommonPrj.sndPlay.PlayingFlags.SND_MEMORY  |  (uint)ArLi.CommonPrj.sndPlay.PlayingFlags.SND_PURGE  |  (uint)ArLi.CommonPrj.sndPlay.PlayingFlags.SND_NODEFAULT);

*/
#endregion

[dllimport("winmm.dll",  entrypoint="mcisendstring")]
internal  static  extern  int  mciSendString(string  lpstrCommand,string  lpstrReturnstring,int  uReturnLength,int  hwndCallback);

#region  how  use  this  ?
/*

int  i  =  sndplay.mcisendstring(@"play  d:\media\midi\bg1.mid",null,0,0);
int  i  =  sndPlay.mciSendString(@"close  D:\Media\Midi\bg1.mid",null,0,0);

*/
#endregion

internal  static  system.byte[]  sound  =  null;
internal  enum  PlayingFlags  :uint  {
SND_SYNC  =  0x00,
SND_ASYNC  =  0x01,
SND_NODEFAULT  =  0x02,
SND_MEMORY  =  0x04,
SND_ALIAS  =  0x010000,
SND_FILENAME  =  0x020000,
SND_RESOURCE  =  0x040004,
SND_ALIAS_ID  =  0x0110000,
SND_ALIAS_START  =  0,
SND_LOOP  =  0x08,
SND_NOSTOP  =  0x010,
SND_VALID  =  0x01F,
SND_NOWAIT  =  0x02000,
SND_PURGE  =  0x40
}
}
}


加系统菜单

using  system;
using  System.Runtime.InteropServices;

namespace  arli.commonprj  {

#region  how  use  this?
/*
*  append  this  to  form  Code  body
*
using  ArLi.CommonPrj;

internal  const  int32  aboutid  =  1000;
protected  override  void  WndProc(ref  Message  m)  {
if(m.Msg  ==  CommonPrj.sysMenu.WM_SYSCOMMAND)
switch(m.WParam.ToInt32())  {
case  aboutID  :
fm_About  Obj  =  new  fm_About();
Obj.ShowDialog(this);
return;
default:
break;
}
base.WndProc(ref  m);
}
*
*  appendmenu  on  form_load
*
//add  menuLine  
sysMenu.AddMenu(this.Handle,0,null);
//add  menuItem
sysMenu.AddMenu(this.Handle,IDM_ABOUT,"About  ...");
*/
#endregion

internal  class  sysmenu  {
internal  sysMenu(){}

[dllimport("user32.dll")]
private  static  extern  IntPtr  GetSystemMenu(IntPtr  hWnd,  bool  bRevert);

[dllimport("user32.dll")]
private  static  extern  bool  AppendMenu  (IntPtr  hMenu,  Int32  wFlags,  Int32  wIDNewItem,  string  lpNewItem);

internal  const  int32  wm_syscommand  =  0x112;
internal  const  Int32  MF_SEPARATOR  =  0x800;
internal  const  Int32  MF_STRING  =  0x0;

internal  static  void  addmenu(intptr  handleof,  int32  menuid,  string  menustrof)  {
IntPtr  sysMenuHandle  =  GetSystemMenu(handleOf,  false);
if  (menuStrOf  ==  null){
AppendMenu(sysMenuHandle,  MF_SEPARATOR,  menuID,  string.Empty);
}else{
AppendMenu(sysMenuHandle,  MF_STRING,  menuID,  menuStrOf);
}
}


}
}

此为资源文件生成器,支持二进制

using  system;
using  System.IO;
using  System.Drawing;
using  System.Resources;
using  System.Reflection;
using  System.Windows.forms;

[assembly:  system.reflection.assemblyversion("1.2.*")]

[assembly:  system.reflection.assemblytitle("resource  build")]
[assembly:  System.Reflection.AssemblyDescription("Resource  with  Binary")]
[assembly:  System.Reflection.AssemblyConfiguration("")]
[assembly:  System.Reflection.AssemblyCompany("ArLi")]
[assembly:  System.Reflection.AssemblyProduct("Resource  Build")]
[assembly:  System.Reflection.AssemblyCopyright("ArLi  build  on  C#")]
[assembly:  System.Reflection.AssemblyTrademark("")]
[assembly:  System.Reflection.AssemblyCulture("")]

class  resadd  {

private  static  assemblycopyrightattribute  objcopyright  =  (assemblycopyrightattribute)assemblycopyrightattribute.getcustomattribute(assembly.getexecutingassembly(),typeof(assemblycopyrightattribute));
private  static  string  myCopyright  =  objCopyright.Copyright.ToString();
private  static  string  myExeName  =  new  FileInfo(Application.ExecutablePath).Name  +  "  ";

private  static  string  myswitch  =  "[switch]:\n"  +
"/fn,fileFullName\t\tResource  FileName(no  Extension)\n"  +
"/img,itemName,fileFullName\tImage  Type  Add  of  Filename  \n\t\t\t\t(support  gif/png/jpg/bmp/ico)\n"  +
"/ico,itemName,fileFullName\tIcon  Type  Add  of  Filename  \n"  +
"/bin,itemName,fileFullName\tBinary  Type  Add  of  FileName";
private  static  string  myExemple  =  "[Exemple]:  Create  c:\\tst.resources\n"  +
myExeName  +
"/fn,c:\\tst  "  +
"/bin,wav1,wav1.wav  "  +
"/bin,wav2,c:\\wav2.wav  "  +
"/img,img1,bmp1.bmp"  +
"\n"  +  myExeName  +  "/fn,c:\\tst  /bin,wav1,\"c:\\winnt\\media\\The  Microsoft  Sound.wav\"";

[stathread]
public  static  void  Main(string[]  args)  {
if  (args.Length>0){
if(args[0]  ==  "/?"){
ShowHelp(null);
}else{
string[]  filenameOf  =  args[0].Split(',');
if  (filenameOf[0]  ==  "/fn"  &&  filenameOf.Length  >=  2){
if  (filenameOf[1]  ==  ""){
ShowHelp("u  must  input  filename  of  /fn,fileFullName  !");
}else{
string  theResourcesFileName  =  filenameOf[1]  +  ".resources";
ResourceWriter  rw  =  new  ResourceWriter(theResourcesFileName);

for(int  i=1;  i<args.length;  i++){
string[]  addBody  =  args[i].Split(',');
if  (addBody.Length  <  3){
Console.WriteLine("Invalid  switch  on:  "  +  args[i]);
}else{
FileInfo  ObjFile  =  new  FileInfo(addBody[2]);
if  (!  ObjFile.Exists){
Console.WriteLine("File  "  +  ObjFile.FullName  +  "  no  Exists!!!  skip  this.");
}else{
switch  (addBody[0].ToUpper()){
case  "/IMG":
ImageAdd(rw,addBody[1],ObjFile.FullName);
Console.WriteLine("append  Type:Image  of  "  +  addBody[1]  +  "  ("  +  addBody[2]  +  ")");
break;
case  "/ICO":
IconAdd(rw,addBody[1],ObjFile.FullName);
Console.WriteLine("append  Type:Icon  of  "  +  addBody[1]  +  "  ("  +  addBody[2]  +  ")");
break;
case  "/BIN":
BinaryAdd(rw,addBody[1],ObjFile.FullName);
Console.WriteLine("append  Type:Binary  of  "  +  addBody[1]  +  "  ("  +  addBody[2]  +  ")");
break;

default:
ShowHelp("u  must  input  resources  type  !");
break;
}
}
}
}

console.writeline("\nbuild  of  "  +  theresourcesfilename  +  "  complete.");
rw.Generate();
}
}else{
ShowHelp("need  filename  of  /fn,fileFullName  !");
}
}
}else{
ShowHelp(null);
Console.WriteLine("\nPress  Enter  to  end  ..");
Console.Read();
}
}

private  static  void  showhelp(string  errmsg)  {
Version  PrjVer  =  new  Version(Application.ProductVersion);

console.writeline(""  +

application.productname.tostring()  +
"  v"  +  PrjVer.Major.ToString()  +  "."  +  PrjVer.Minor.ToString()  +  "."  +  PrjVer.Build.ToString()  +
"  Studio:  "  +  Application.CompanyName  +
"  Copyright:  "  +  myCopyright  +

"\n");

if  (errmsg  !=  null){console.writeline("waring  !!\n"  +  errmsg  +  "\n");}

console.writeline(myexemple  +  "\n\n"  +  myswitch);
}

private  static  void  binaryadd(resourcewriter  rw,string  itemnameof,string  filenameof)  {
try{
FileStream  fs  =  new  FileStream(filenameOf,FileMode.Open,  FileAccess.Read);
int  byteLength  =  (int)fs.Length;
byte[]  wf  =  new  byte[byteLength];
fs.Read(wf,0,byteLength);
fs.Close();

rw.addresource(itemnameof,  wf);
}catch  (Exception  e){
Console.WriteLine(e.ToString());
}
}

private  static  void  imageadd(resourcewriter  rw,string  itemnameof,string  filenameof)  {
try{
Image  b  =  Image.FromFile(filenameOf);
rw.AddResource(itemnameOf,  b);
}catch  (Exception  e){
Console.WriteLine(e.ToString());
}
}

private  static  void  iconadd(resourcewriter  rw,string  itemnameof,string  filenameof)  {
try{
Icon  b  =  new  Icon(filenameOf);
rw.AddResource(itemnameOf,  b);
}catch  (Exception  e){
Console.WriteLine(e.ToString());
}
}
}


程序,我以前有个习惯不使用资源而是将内容二进制打进程序
直接使用  byte[]  bgSound  =  new  byte[]{....}  //此为声音文件二进制,原来是用vc  现在特用c#  重写了一个

using  system;
using  System.IO;
//using  System.Drawing;
//using  System.Resources;
using  System.Reflection;
using  System.Windows.forms;
using  System.Text.RegularExpressions;

[assembly:  system.reflection.assemblyversion("1.5.*")]

[assembly:  system.reflection.assemblytitle("string  build")]
[assembly:  System.Reflection.AssemblyDescription("System.byte[]  to  string")]
[assembly:  System.Reflection.AssemblyConfiguration("")]
[assembly:  System.Reflection.AssemblyCompany("ArLi")]
[assembly:  System.Reflection.AssemblyProduct("string  Build")]
[assembly:  System.Reflection.AssemblyCopyright("ArLi  build  on  C#")]
[assembly:  System.Reflection.AssemblyTrademark("")]
[assembly:  System.Reflection.AssemblyCulture("")]

class  resadd  {

private  static  assemblycopyrightattribute  objcopyright  =  (assemblycopyrightattribute)assemblycopyrightattribute.getcustomattribute(assembly.getexecutingassembly(),typeof(assemblycopyrightattribute));
private  static  string  myCopyright  =  objCopyright.Copyright.ToString();
private  static  string  myExeName  =  new  FileInfo(Application.ExecutablePath).Name  +  "  ";

private  static  string  myhelpbody  =  ""  +

"[syntax]:\n"  +
myExeName  +  "formFile  variableName  toFile  \tsave  string  to  file\n"  +
myExeName  +  "formFile  variableName  /copy  \tcopy  string  to  clipboard.\n"  +
myExeName  +  "formFile  variableName  /copy  /s  \tcopy  string  &  show  stats.\n\n"  +

"[exemple]:\n"  +
myExeName  +  "c:\\1.ico  ico_1  /copy  /s";

[stathread]
public  static  void  Main(string[]  args)  {
if  (args.Length>2){
if(args[0]  ==  "/?"){
ShowHelp(null);
}else{
try{
FileInfo  fi  =  new  FileInfo(args[0]);
Match  MatchVariableName  =  Regex.Match(args[1],@"\b([a-zA-Z]\w*)",RegexOptions.IgnoreCase|RegexOptions.IgnorePatternWhitespace|RegexOptions.ExplicitCapture);
string  variableName  =  MatchVariableName.ToString();
if  (args[1].Substring(0,1)  ==  "/"){
Console.WriteLine("don't  forget  VariableName!!!");
}
else  if  (!  fi.Exists){
Console.WriteLine("File  "  +  fi.FullName  +  "  no  Exists!!!");
}
else  if  (variableName  ==  ""){
Console.WriteLine("invalid  variableName!!");
}
else{
FileStream  fs  =  new  FileStream(fi.FullName,FileMode.Open,  FileAccess.Read);
int  byteLength  =  (int)fs.Length;
byte[]  wf  =  new  byte[byteLength];
fs.Read(wf,0,byteLength);
fs.Close();
fs  =  null;

int  i  =  0;
byte  iCr  =  0;
bool  ShowStats  =  (args.Length>3  &&  args[3]  ==  "/s");

string  s  =  "system.byte[]  "  +  variablename  +  "  =  new  system.byte[]  {";
string  sp  =  new  string('  ',byteLength.ToString().Length);

console.write("poc  "  +  s  +  ""  +  bytelength.tostring()  +  "  byte}  ...\nuse  ctrl+break  can  stop  ...\n");

s  +=  "\r\n";

if  (showstats){
while(i  <  byteLength){
s  +=  wf[i]  +  ",";
if  (iCr  ==  25){
s  +=  "\r\n";
iCr=0;
}
i++;
iCr++;
Console.Write("\r"  +  i  +  sp);
}
}else{
while(i  <  byteLength){
s  +=  wf[i]  +  ",";
if  (iCr  ==  25){
s  +=  "\r\n";
iCr=0;
}
i++;
iCr++;
}
}

s  =  s.remove(s.length-1,1)  +  "\r\n};";
s  =  "\t#region  System.Byte[]  "  +  variableName  +  "  =  new  System.Byte[]  {...}\r\n"  +  s  +  "\r\n\t#endregion";

if  (args[2]  ==  "/copy")  {
Clipboard.SetDataObject(s,true);
}else{
FileInfo  ObjFile  =  new  FileInfo(args[2]);
StreamWriter  sw  =  ObjFile.CreateText();
sw.Write(s);
sw.Close();
}
Console.WriteLine("\rdone..  ^o^");
}
}catch  (Exception  e){
Console.WriteLine(e.Message.ToString());
}
}
}else{
ShowHelp(null);
Console.WriteLine("\nPress  Enter  to  end  ..");
Console.Read();
}
}

private  static  void  showhelp(string  errmsg)  {
Version  PrjVer  =  new  Version(Application.ProductVersion);

console.writeline(""  +

application.productname.tostring()  +
"  v"  +  PrjVer.Major.ToString()  +  "."  +  PrjVer.Minor.ToString()  +  "."  +  PrjVer.Build.ToString()  +
"  Studio:  "  +  Application.CompanyName  +
"  Copyright:  "  +  myCopyright  +

"\n");

if  (errmsg  !=  null){console.writeline("waring  !!\n"  +  errmsg  +  "\n");}

console.writeline(myhelpbody);
}
}  

Add by : Huobazi (2005-10-06:05:59)