C#中计算字符串长度,一个中文算两个字符int len1 = System.Text.Encoding.Default.GetBytes("aaa").Length; //结果为 3int len2 = System.Text.Encoding.Default.GetBytes("张三丰").Length; /……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3222浏览 1309个赞
这段代码从控制台接收一个字符串,然后判断该字符串是否可以被转换成整型,用到int.TryParsestring i = Console.ReadLine(); int a=0; if (int.TryParse(i, out a) == false) //判断是否可以转换为整型 {……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1497浏览 595个赞
这段代码是一个IIS操作类,可以最IIS进行站点的添加、删除、修改等操作//IISHelper.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;us……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2071浏览 1314个赞
C# 获得当前运行程序所在的目录using System;using System.IO; class DirectoryCurrent{ public static void Main() { Console.WriteLine ("Current directory: \"{0}\""……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3070浏览 1413个赞
C# 获得目录创建时间,在这段代码中先创建一个目录,获取创建时间后,将目录删除using System;using System.IO; class DirectoryCreationTime{ public static void Main() { string directoryString = ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1462浏览 2891个赞
C#创建和删除目录using System;using System.IO; class DirectoryCreation{ public static void Main() { string directoryString = Directory.GetCurrentDirectory() +……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1649浏览 906个赞
C#清除IE浏览器缓存,IE的临时文件夹using System.IO; ...void clearIECache(){ ClearFolder (new DirectoryInfo (Environment.GetFolderPath (Environment.SpecialFolder.InternetCache)));}……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2137浏览 2908个赞
C# 创建Group Object in Active Directoryusing System.DirectoryServices;using System.Reflection; public class ADGroup{ private String ADGRPOUP = "group"; // ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3189浏览 2684个赞
using System.Data;using System.Data.SqlClient;…// Substitute your connection string below in conxString String conxString = “Data Source=MYSERVER; Integrated Securit……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1453浏览 2549个赞
C#检测网络驱动器的剩余磁盘空间using System;using System.Management; class NetworkSpace{ static void Main(string[] args) { SelectQuery query = new SelectQuery( "select……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1950浏览 1926个赞
C#检测光驱里的光盘是否已经加载using System;using System.Management; class App{ public static void Main() { SelectQuery query = new SelectQuery( "select * from win32_logicaldi……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3120浏览 1295个赞
这段代码演示了C#如何通过修改AssemblyInfo.cs文件实现自动版本号// Modify the AssemblyVersion line in the // "AssemblyInfo.cs" file: // Add a "*" to the section you want to increa……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3226浏览 1650个赞
C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用using System;using System.Management; namespace CDROMManagement{ class WMIEvent { static void Main(string[] args) ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2958浏览 2655个赞
本代码创建了一个类的索引器用来访问类,就像访问数组一样using System.Collections.Generic;...class Client{ private Dictionary<int, string> invoices = new Dictionary<int, string>(); ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2714浏览 2029个赞
本代码通过ICollection接口的count属性来输出其元素数量using System;using System.Collections; public class CountArray{ public static void Main() { string[] strings = {"Ajax&quo……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2651浏览 1218个赞
这段代码通过BitArray来计算素数using System.Collections;...const int LAST_CANDIDATE = 1000;int primes = 0;BitArray candidates = new BitArray (LAST_CANDIDATE, true); for (int i = 2; i ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3026浏览 1476个赞
本范例演示了C#中如何在不打开dos窗口的情况下运行控制台程序,同时捕获程序的输出信息// This snippet needs the "System.Diagnostics"// library // Application path and command line argumentsstring Applicatio……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1519浏览 979个赞
C#枚举类型用于标志位,可以进行与和或操作enum Mammalia{ Bison = 1, Human = 2, Mammal = 255} public class MammalCheck{ public static void Main( ) { //Mammalia pattern……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2403浏览 1643个赞
本代码演示了C#自带的Array.Sort方法排序using System; public class ReverseArraySort{ public static void Main() { string[] strings = {"beta", "alpha", "……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1882浏览 877个赞
这段代码演示了C#使用二分法快速搜索数组using System; class ArrayBinarySearch{ public static void Main() { int[] ints = {0, 10, 100, 1000, 1000000 }; Console.WriteLine ("A……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2255浏览 2461个赞
这段代码可以把C#的数组倒置过来,主要用到了Array.Reverse方法using System; public class ReverseArray{ public static void Main() { string[] strings = {"beta", "alpha"……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3228浏览 2565个赞
C# 读取控制台输入的信息char character = (char) Console.Read(); // returns an int string stringInput = Console.ReadLine();……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3400浏览 2442个赞
C# 给函数传入匿名委托并控制执行指定的次数public delegate int ConsoleDelegate (int value); public class Test{ void ExecuteDelegate (ConsoleDelegate cd, int iterations) { for (int i=1……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2346浏览 1696个赞
C# 重写ToString()方法输出私有变量的代码片段public class MyClass{ private string customer =""; private int customerID = 0; public string Customer { get { return c……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3156浏览 1727个赞
C# 重载加号运算符计算两个对象的和public class ValidFloat{ private float value; private bool valid; public ValidFloat (float value, bool valid) { this.value = value; ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3194浏览 1786个赞
模仿老版本的windows上的夜空屏保代码using System;class set_cursor2{ int lastnum; private int RandomNumber(int min, int max){ Random random = new Random(); lastnum = random.Next(mi……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2380浏览 2774个赞
C# 测试代码执行时间的两个方法DateTime startTime = DateTime.Now;Console.WriteLine ("Started: {0}", startTime); // Execute the task to be timedfor (int i=1; i < 100000; i++){}……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1745浏览 1223个赞
本代码片段演示了C#中如何实现IDispose接口,实现资源释放public class MyClass : IDisposable{ public void Dispose() { // release any resources here // when object is about to be garb……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3052浏览 1794个赞
C#获取 NetBIOS和 DNS计算机名static string GetLocalHostName (){ string netBiosName = System.Environment.MachineName; //return netBiosName; // Following method is deprecated……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3224浏览 310个赞
C#代码验证IP地址,包含格式和是否超过255的限制/// <summary> /// Check IP Address, will accept 0.0.0.0 as a valid IP /// </summary> /// <param name="strIP……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2532浏览 552个赞
C# 从控制台读取数字try{ int number = Convert.ToInt32 (Console.ReadLine()); Console.WriteLine ("Number: " + number);}catch (FormatException e){ Console.WriteLine (……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2980浏览 659个赞
下面的c#代码片段显示产生随机数输出到控制台。RandomNumber函数的第一个参数为生成的随机数的最小值。第二个参数是最大值+ 1。using System; class Program{ private Random random = new Random(); private int RandomNumber (int ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3104浏览 168个赞
根据给定的网页url地址返回域名/* ** Method 1 (using the build-in Uri-object)*/public static string ExtractDomainNameFromURL_Method1(string Url){ if (!Url.Contains("://")) ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3289浏览 813个赞
下面的c#代码片段使用不同的格式显示日期和时间。using C = System.Console; ...static void Main() { DateTime dateTime = DateTime.Now; C.WriteLine ("d = {0:d}", dateTime ); // mm/dd/yyy……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1442浏览 1664个赞
下面的c#代码片段演示了各种字符串的方法和属性。using System; class StringMethods{ static void Main() { char[] characterArray; int position; string result, string1; s……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2920浏览 1765个赞
C# 定义一个简单的事件(Event)public class MyClass : IDisposable{ public event EventHandler Disposing; public void Dispose() { // release any resources here if (D……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2765浏览 1168个赞
本范例演示了C#如何对一个给定的字符串进行MD5加密/// <summary>/// Calculates a MD5 hash from the given string and uses the given/// encoding./// </summary>/// <param name="Inpu……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2479浏览 2427个赞
C#提供了两种方式定义多行字符串变量string couplet1 = "Half a league, half a league,\n" + "Half a league onward,"; string couplet2 = @"All in the valley……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2795浏览 2390个赞
C# 声明变量并赋值的代码演示class VariableDeclarations{ public static void Main() { // Type Identifier Assignment (maximum value) bool aBool; aBool = true; ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 1543浏览 2099个赞
本范例介绍了C#中const的用法和一些常量声明class ConstantDeclarations{ public static void Main() { // Type Identifier Initializer const bool aBool = true; co……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2404浏览 2139个赞
本范例演示了C#开发GUI程序时如何避免一个超长处理过程占用全部资源,导致界面不能响应用户事件的方法// You need the Threading library to use the // "Thread.Sleep" function// using System.Threading; // This boolean……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2994浏览 1309个赞
本代码演示了如何创建一个unsafe的指针数组记录struct的地址信息class UnsafePointerArray{ public struct AStruct { public int anInteger; } public static void CreatePointerArray() { ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3210浏览 1734个赞
本范例演示怎样通过c#中的XmlSerializer序列化一个普通对象// This is the test class we want to // serialize:[Serializable()]public class TestClass{ private string someString; public string……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3074浏览 545个赞
C#创建多维数组代码using System; public class MultidimensionalArray{ public static void Main() { int[] lowerBounds = {1, 2, 4}; int[] lengths = {4, 2, 1}; ……继续阅读 » 水墨上仙 5年前 (2021-03-14) 2231浏览 2071个赞
C# 里的MD5 算法代码片段using System; public string CreateMD5Hash (string input){ // Use input string to calculate MD5 hash MD5 md5 = System.Security.Cryptography.MD5.Create();……继续阅读 » 水墨上仙 5年前 (2021-03-14) 3053浏览 1685个赞