csharp 代码片段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 代码片段相关的知识,希望对你有一定的参考价值。

/// <summary>
/// Give back a random text of the given length
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GetRandomText(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}


public static string GetRandomSpecials(int length)
{
    const string chars = "!@#$%^&*()";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());
}


/// <summary>
/// Generate Random Code of defined Length [ Alphabets + Numbers + Symbols ( Upper and Lower case ) ]
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GetRandomDynamic(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz!@#$%^*()+_|?:";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());

}


/// <summary>
/// Generate Random Code of defined Length [ Alphabets + Numbers ( Upper and Lower case ) ]
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string GetRandomAlphanumeric(int length)
{
    const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
    var random = new Random();
    return new string(Enumerable.Repeat(chars, length)
      .Select(s => s[random.Next(s.Length)]).ToArray());

}


public static string GenerateDatabaseUser()
{
    const string preChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    const string postChar = "0123456789";

    var random = new Random();
    string preOutput = new string(Enumerable.Repeat(preChar, 5)
      .Select(s => s[random.Next(s.Length)]).ToArray());

    string poseOutput = new string(Enumerable.Repeat(postChar, 3)
      .Select(s => s[random.Next(s.Length)]).ToArray());

    return preOutput + poseOutput;
}


// SOURCE: https://stackoverflow.com/a/1344242/3436775

Public enum MimeTypes
{
  [Description("application/postscript")]
  ai,
  [Description("audio/x-aiff")]
  aif,
  [Description("audio/x-aiff")]
  aifc,
  [Description("audio/x-aiff")]
  aiff,
  [Description("text/plain")]
  asc,
  [Description("application/atom+xml")]
  atom,
  [Description("audio/basic")]
  au,
  [Description("video/x-msvideo")]
  avi,
  [Description("application/x-bcpio")]
  bcpio,
  [Description("application/octet-stream")]
  bin,
  [Description("image/bmp")]
  bmp,
  [Description("application/x-netcdf")]
  cdf,
  [Description("image/cgm")]
  cgm,
  [Description("application/octet-stream")]
  class,
  [Description("application/x-cpio")]
  cpio,
  [Description("application/mac-compactpro")]
  cpt,
  [Description("application/x-csh")]
  csh,
  [Description("text/css")]
  css,
  [Description("application/x-director")]
  dcr,
  [Description("video/x-dv")]
  dif,
  [Description("application/x-director")]
  dir,
  [Description("image/vnd.djvu")]
  djv,
  [Description("image/vnd.djvu")]
  djvu,
  [Description("application/octet-stream")]
  dll,
  [Description("application/octet-stream")]
  dmg,
  [Description("application/octet-stream")]
  dms,
  [Description("application/msword")]
  doc,
  [Description("application/xml-dtd")]
  dtd,
  [Description("video/x-dv")]
  dv,
  [Description("application/x-dvi")]
  dvi,
  [Description("application/x-director")]
  dxr,
  [Description("application/postscript")]
  eps,
  [Description("text/x-setext")]
  etx,
  [Description("application/octet-stream")]
  exe,
  [Description("application/andrew-inset")]
  ez,
  [Description("image/gif")]
  gif,
  [Description("application/srgs")]
  gram,
  [Description("application/srgs+xml")]
  grxml,
  [Description("application/x-gtar")]
  gtar,
  [Description("application/x-hdf")]
  hdf,
  [Description("application/mac-binhex40")]
  hqx,
  [Description("text/html")]
  htm,
  [Description("text/html")]
  html,
  [Description("x-conference/x-cooltalk")]
  ice,
  [Description("image/x-icon")]
  ico,
  [Description("text/calendar")]
  ics,
  [Description("image/ief")]
  ief,
  [Description("text/calendar")]
  ifb,
  [Description("model/iges")]
  iges,
  [Description("model/iges")]
  igs,
  [Description("application/x-java-jnlp-file")]
  jnlp,
  [Description("image/jp2")]
  jp2,
  [Description("image/jpeg")]
  jpe,
  [Description("image/jpeg")]
  jpeg,
  [Description("image/jpeg")]
  jpg,
  [Description("application/x-javascript")]
  js,
  [Description("audio/midi")]
  kar,
  [Description("application/x-latex")]
  latex,
  [Description("application/octet-stream")]
  lha,
  [Description("application/octet-stream")]
  lzh,
  [Description("audio/x-mpegurl")]
  m3u,
  [Description("audio/mp4a-latm")]
  m4a,
  [Description("audio/mp4a-latm")]
  m4b,
  [Description("audio/mp4a-latm")]
  m4p,
  [Description("video/vnd.mpegurl")]
  m4u,
  [Description("video/x-m4v")]
  m4v,
  [Description("image/x-macpaint")]
  mac,
  [Description("application/x-troff-man")]
  man,
  [Description("application/mathml+xml")]
  mathml,
  [Description("application/x-troff-me")]
  me,
  [Description("model/mesh")]
  mesh,
  [Description("audio/midi")]
  mid,
  [Description("audio/midi")]
  midi,
  [Description("application/vnd.mif")]
  mif,
  [Description("video/quicktime")]
  mov,
  [Description("video/x-sgi-movie")]
  movie,
  [Description("audio/mpeg")]
  mp2,
  [Description("audio/mpeg")]
  mp3,
  [Description("video/mp4")]
  mp4,
  [Description("video/mpeg")]
  mpe,
  [Description("video/mpeg")]
  mpeg,
  [Description("video/mpeg")]
  mpg,
  [Description("audio/mpeg")]
  mpga,
  [Description("application/x-troff-ms")]
  ms,
  [Description("model/mesh")]
  msh,
  [Description("video/vnd.mpegurl")]
  mxu,
  [Description("application/x-netcdf")]
  nc,
  [Description("application/oda")]
  oda,
  [Description("application/ogg")]
  ogg,
  [Description("image/x-portable-bitmap")]
  pbm,
  [Description("image/pict")]
  pct,
  [Description("chemical/x-pdb")]
  pdb,
  [Description("application/pdf")]
  pdf,
  [Description("image/x-portable-graymap")]
  pgm,
  [Description("application/x-chess-pgn")]
  pgn,
  [Description("image/pict")]
  pic,
  [Description("image/pict")]
  pict,
  [Description("image/png")]
  png,
  [Description("image/x-portable-anymap")]
  pnm,
  [Description("image/x-macpaint")]
  pnt,
  [Description("image/x-macpaint")]
  pntg,
  [Description("image/x-portable-pixmap")]
  ppm,
  [Description("application/vnd.ms-powerpoint")]
  ppt,
  [Description("application/postscript")]
  ps,
  [Description("video/quicktime")]
  qt,
  [Description("image/x-quicktime")]
  qti,
  [Description("image/x-quicktime")]
  qtif,
  [Description("audio/x-pn-realaudio")]
  ra,
  [Description("audio/x-pn-realaudio")]
  ram,
  [Description("image/x-cmu-raster")]
  ras,
  [Description("application/rdf+xml")]
  rdf,
  [Description("image/x-rgb")]
  rgb,
  [Description("application/vnd.rn-realmedia")]
  rm,
  [Description("application/x-troff")]
  roff,
  [Description("text/rtf")]
  rtf,
  [Description("text/richtext")]
  rtx,
  [Description("text/sgml")]
  sgm,
  [Description("text/sgml")]
  sgml,
  [Description("application/x-sh")]
  sh,
  [Description("application/x-shar")]
  shar,
  [Description("model/mesh")]
  silo,
  [Description("application/x-stuffit")]
  sit,
  [Description("application/x-koan")]
  skd,
  [Description("application/x-koan")]
  skm,
  [Description("application/x-koan")]
  skp,
  [Description("application/x-koan")]
  skt,
  [Description("application/smil")]
  smi,
  [Description("application/smil")]
  smil,
  [Description("audio/basic")]
  snd,
  [Description("application/octet-stream")]
  so,
  [Description("application/x-futuresplash")]
  spl,
  [Description("application/x-wais-source")]
  src,
  [Description("application/x-sv4cpio")]
  sv4cpio,
  [Description("application/x-sv4crc")]
  sv4crc,
  [Description("image/svg+xml")]
  svg,
  [Description("application/x-shockwave-flash")]
  swf,
  [Description("application/x-troff")]
  t,
  [Description("application/x-tar")]
  tar,
  [Description("application/x-tcl")]
  tcl,
  [Description("application/x-tex")]
  tex,
  [Description("application/x-texinfo")]
  texi,
  [Description("application/x-texinfo")]
  texinfo,
  [Description("image/tiff")]
  tif,
  [Description("image/tiff")]
  tiff,
  [Description("application/x-troff")]
  tr,
  [Description("text/tab-separated-values")]
  tsv,
  [Description("text/plain")]
  txt,
  [Description("application/x-ustar")]
  ustar,
  [Description("application/x-cdlink")]
  vcd,
  [Description("model/vrml")]
  vrml,
  [Description("application/voicexml+xml")]
  vxml,
  [Description("audio/x-wav")]
  wav,
  [Description("image/vnd.wap.wbmp")]
  wbmp,
  [Description("application/vnd.wap.wbxml")]
  wbmxl,
  [Description("text/vnd.wap.wml")]
  wml,
  [Description("application/vnd.wap.wmlc")]
  wmlc,
  [Description("text/vnd.wap.wmlscript")]
  wmls,
  [Description("application/vnd.wap.wmlscriptc")]
  wmlsc,
  [Description("model/vrml")]
  wrl,
  [Description("image/x-xbitmap")]
  xbm,
  [Description("application/xhtml+xml")]
  xht,
  [Description("application/xhtml+xml")]
  xhtml,
  [Description("application/vnd.ms-excel")]
  xls,
  [Description("application/xml")]
  xml,
  [Description("image/x-xpixmap")]
  xpm,
  [Description("application/xml")]
  xsl,
  [Description("application/xslt+xml")]
  xslt,
  [Description("application/vnd.mozilla.xul+xml")]
  xul,
  [Description("image/x-xwindowdump")]
  xwd,
  [Description("chemical/x-xyz")]
  xyz,
  [Description("application/zip")]
  zip
}
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
   out ulong lpFreeBytesAvailable,
   out ulong lpTotalNumberOfBytes,
   out ulong lpTotalNumberOfFreeBytes);



private static void SpaceCalculator()
{
    ulong FreeBytesAvailable;
    ulong TotalNumberOfBytes;
    ulong TotalNumberOfFreeBytes;

    bool success = GetDiskFreeSpaceEx(@"D:\SC_Backup",
                             out FreeBytesAvailable,
                             out TotalNumberOfBytes,
                             out TotalNumberOfFreeBytes);
    if (!success)
        throw new System.ComponentModel.Win32Exception();

    Console.WriteLine("Free Bytes Available:      {0,15:D}", FreeBytesAvailable);
    Console.WriteLine("Total Number Of Bytes:     {0,15:D}", TotalNumberOfBytes);
    Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
}
public class ByteConverter
{
    public FileSizeInfo CalculateBye( double fileSize)
    {
        FileSizeInfo objFileSizeInfo = new FileSizeInfo ();
        if (fileSize < 1024)
        {
            objFileSizeInfo.Size = fileSize;
            objFileSizeInfo.SizeName = "Byte";
        }
        else if (fileSize < 1024 * 1024)
        {
            objFileSizeInfo.Size = fileSize / 1024.00;
            objFileSizeInfo.SizeName = "KB";
        }
        else if (fileSize < 1024 * 1024 * 1024)
        {
            objFileSizeInfo.Size = fileSize / (1024 * 1024);
            objFileSizeInfo.SizeName = "MB";
        }
        else
        {
            objFileSizeInfo.Size = fileSize / (1024 * 1024 * 1024);
            objFileSizeInfo.SizeName = "GB";
        }
        return objFileSizeInfo;
    }
}


public class FileSizeInfo
{
    public double Size { get; set; }
    public string SizeName { get; set; }
}

[DllImport("wininet.dll")]
private extern static bool InternetGetConnectedState( out int Description, int ReservedValue);
public static bool InternetStateOK()
{
       int desc;
       return InternetGetConnectedState( out desc, 0);
}
//Full Path till the executable, In case of shadow copies, this will be the temp directory
System.Reflection.Assembly.GetExecutingAssembly().Location;

//Permanent path of the assembly
System.Reflection.Assembly.GetExecutingAssembly().CodeBase;

//Local Codebase Path
new Uri(System.IO.Path.GetDirectoryName(codeBase)).LocalPath;

//Full Path till the directory
System.IO.Path.GetDirectoryName(path);
System.Environment.CurrentDirectory;

//Normally : 
System.IO.Directory.GetCurrentDirectory();
AppDomain.CurrentDomain.BaseDirectory; //Additional Backslash in the end

// Image to Byte Array
public byte[] imageToByteArray(System.Drawing. Image imageIn)
 {
     MemoryStream ms = new MemoryStream();
     imageIn.Save(ms, System.Drawing.Imaging. ImageFormat.Gif);
     return ms.ToArray();
 }

// Byte Array to Image
public Image byteArrayToImage(byte [] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}
using System.Collections.Generic;

namespace SharePortfolio.Core.Entity
{
    /// <summary>
    /// Perform Create, Read, Update, Delete Operations on Generic Items
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public interface ICRUD<T> where T : class
    {
        List<T> List { get; }

        object Insert(T entity);

        T FindByID(int ID);

        T FindByItem(object item);

        void Update(T entity);

        void DeleteByID(T entity);
    }
}


// IMPLEMENTATION
using System;
using System.Collections.Generic;
using SharePortfolio.Core.Entity;

namespace SharePortfolio.Repository.Service
{
    public class CRUDShareStock : ICRUD<ICompanyStockDetail>
    {
        ShareStockDataProvider dataProvider = new ShareStockDataProvider();
        public List<ICompanyStockDetail> List
        {
            get
            {
                return dataProvider.GetAllStockDetails();
            }
        }


        public void DeleteByID(ICompanyStockDetail entity)
        {
            throw new NotImplementedException();
        }

        public ICompanyStockDetail FindByID(int ID)
        {
            throw new NotImplementedException();
        }

        public ICompanyStockDetail FindByItem(object item)
        {
            throw new NotImplementedException();
        }

        public object Insert(ICompanyStockDetail entity)
        {
            throw new NotImplementedException();
        }

        public void Update(ICompanyStockDetail entity)
        {
            throw new NotImplementedException();
        }
    }
}


以上是关于csharp 代码片段的主要内容,如果未能解决你的问题,请参考以下文章

csharp 膨胀片段

csharp 线程和异步任务片段

csharp 测试片段

csharp c#test片段

csharp Epicor标准练习片段

csharp .net片段