本代码片段演示了C#中字符串格式化函数string.Format的使用的方法string sr1 = "75271.com";string sr2 = "脚本分享网";string ss = string.Format("本站域名是:{0},站名是:{1},欢迎光临", sr1, sr2)……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2628浏览 2964个赞
本代码片段演示了使用冒泡法,插入排序法,选择排序法,希尔排序法,快速排序法对C#数组进行排序,是用C#学习数据结构的好代码片段 using System; namespace DataStruct { public class Sorter { /// <summary> ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3113浏览 1183个赞
这段代码可以抓取指定的url的网络图片,并保存到本地 public Bitmap Get_img() { Bitmap img = null; HttpWebRequest req; HttpWebResponse res = null; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1860浏览 508个赞
C#中计算字符串长度,一个中文算两个字符int len1 = System.Text.Encoding.Default.GetBytes("aaa").Length; //结果为 3int len2 = System.Text.Encoding.Default.GetBytes("张三丰").Length; /……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2796浏览 1455个赞
这段代码从控制台接收一个字符串,然后判断该字符串是否可以被转换成整型,用到int.TryParsestring i = Console.ReadLine(); int a=0; if (int.TryParse(i, out a) == false) //判断是否可以转换为整型 {……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2778浏览 923个赞
这段代码是一个IIS操作类,可以最IIS进行站点的添加、删除、修改等操作//IISHelper.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;us……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2379浏览 1323个赞
C# 获得当前运行程序所在的目录using System;using System.IO; class DirectoryCurrent{ public static void Main() { Console.WriteLine ("Current directory: \"{0}\""……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1676浏览 2590个赞
C# 获得目录创建时间,在这段代码中先创建一个目录,获取创建时间后,将目录删除using System;using System.IO; class DirectoryCreationTime{ public static void Main() { string directoryString = ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1348浏览 942个赞
C#创建和删除目录using System;using System.IO; class DirectoryCreation{ public static void Main() { string directoryString = Directory.GetCurrentDirectory() +……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1566浏览 2421个赞
C#清除IE浏览器缓存,IE的临时文件夹using System.IO; ...void clearIECache(){ ClearFolder (new DirectoryInfo (Environment.GetFolderPath (Environment.SpecialFolder.InternetCache)));}……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2497浏览 737个赞
C# 创建Group Object in Active Directoryusing System.DirectoryServices;using System.Reflection; public class ADGroup{ private String ADGRPOUP = "group"; // ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2055浏览 2654个赞
using System.Data;using System.Data.SqlClient;…// Substitute your connection string below in conxString String conxString = “Data Source=MYSERVER; Integrated Securit……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2731浏览 1131个赞
C#检测网络驱动器的剩余磁盘空间using System;using System.Management; class NetworkSpace{ static void Main(string[] args) { SelectQuery query = new SelectQuery( "select……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1961浏览 1618个赞
C#检测光驱里的光盘是否已经加载using System;using System.Management; class App{ public static void Main() { SelectQuery query = new SelectQuery( "select * from win32_logicaldi……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1942浏览 1708个赞
这段代码演示了C#如何通过修改AssemblyInfo.cs文件实现自动版本号// Modify the AssemblyVersion line in the // "AssemblyInfo.cs" file: // Add a "*" to the section you want to increa……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2281浏览 1225个赞
C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用using System;using System.Management; namespace CDROMManagement{ class WMIEvent { static void Main(string[] args) ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1238浏览 2583个赞
本代码创建了一个类的索引器用来访问类,就像访问数组一样using System.Collections.Generic;...class Client{ private Dictionary<int, string> invoices = new Dictionary<int, string>(); ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2869浏览 2886个赞
本代码通过ICollection接口的count属性来输出其元素数量using System;using System.Collections; public class CountArray{ public static void Main() { string[] strings = {"Ajax&quo……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2774浏览 1731个赞
这段代码通过BitArray来计算素数using System.Collections;...const int LAST_CANDIDATE = 1000;int primes = 0;BitArray candidates = new BitArray (LAST_CANDIDATE, true); for (int i = 2; i ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1765浏览 2723个赞
本范例演示了C#中如何在不打开dos窗口的情况下运行控制台程序,同时捕获程序的输出信息// This snippet needs the "System.Diagnostics"// library // Application path and command line argumentsstring Applicatio……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2143浏览 2185个赞
C#枚举类型用于标志位,可以进行与和或操作enum Mammalia{ Bison = 1, Human = 2, Mammal = 255} public class MammalCheck{ public static void Main( ) { //Mammalia pattern……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1769浏览 954个赞
本代码演示了C#自带的Array.Sort方法排序using System; public class ReverseArraySort{ public static void Main() { string[] strings = {"beta", "alpha", "……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2324浏览 1903个赞
这段代码演示了C#使用二分法快速搜索数组using System; class ArrayBinarySearch{ public static void Main() { int[] ints = {0, 10, 100, 1000, 1000000 }; Console.WriteLine ("A……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1450浏览 1609个赞
这段代码可以把C#的数组倒置过来,主要用到了Array.Reverse方法using System; public class ReverseArray{ public static void Main() { string[] strings = {"beta", "alpha"……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2123浏览 1116个赞
C# 读取控制台输入的信息char character = (char) Console.Read(); // returns an int string stringInput = Console.ReadLine();……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2098浏览 838个赞
C# 给函数传入匿名委托并控制执行指定的次数public delegate int ConsoleDelegate (int value); public class Test{ void ExecuteDelegate (ConsoleDelegate cd, int iterations) { for (int i=1……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1613浏览 1455个赞
C# 重写ToString()方法输出私有变量的代码片段public class MyClass{ private string customer =""; private int customerID = 0; public string Customer { get { return c……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1716浏览 2290个赞
C# 重载加号运算符计算两个对象的和public class ValidFloat{ private float value; private bool valid; public ValidFloat (float value, bool valid) { this.value = value; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2759浏览 2992个赞
模仿老版本的windows上的夜空屏保代码using System;class set_cursor2{ int lastnum; private int RandomNumber(int min, int max){ Random random = new Random(); lastnum = random.Next(mi……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2215浏览 849个赞
C# 测试代码执行时间的两个方法DateTime startTime = DateTime.Now;Console.WriteLine ("Started: {0}", startTime); // Execute the task to be timedfor (int i=1; i < 100000; i++){}……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1127浏览 1636个赞
本代码片段演示了C#中如何实现IDispose接口,实现资源释放public class MyClass : IDisposable{ public void Dispose() { // release any resources here // when object is about to be garb……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2107浏览 938个赞
C#获取 NetBIOS和 DNS计算机名static string GetLocalHostName (){ string netBiosName = System.Environment.MachineName; //return netBiosName; // Following method is deprecated……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2717浏览 1506个赞
C#代码验证IP地址,包含格式和是否超过255的限制/// <summary> /// Check IP Address, will accept 0.0.0.0 as a valid IP /// </summary> /// <param name="strIP……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2149浏览 2712个赞
C# 从控制台读取数字try{ int number = Convert.ToInt32 (Console.ReadLine()); Console.WriteLine ("Number: " + number);}catch (FormatException e){ Console.WriteLine (……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1974浏览 2438个赞
下面的c#代码片段显示产生随机数输出到控制台。RandomNumber函数的第一个参数为生成的随机数的最小值。第二个参数是最大值+ 1。using System; class Program{ private Random random = new Random(); private int RandomNumber (int ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2180浏览 647个赞
根据给定的网页url地址返回域名/* ** Method 1 (using the build-in Uri-object)*/public static string ExtractDomainNameFromURL_Method1(string Url){ if (!Url.Contains("://")) ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2820浏览 1044个赞
下面的c#代码片段使用不同的格式显示日期和时间。using C = System.Console; ...static void Main() { DateTime dateTime = DateTime.Now; C.WriteLine ("d = {0:d}", dateTime ); // mm/dd/yyy……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3040浏览 1629个赞
下面的c#代码片段演示了各种字符串的方法和属性。using System; class StringMethods{ static void Main() { char[] characterArray; int position; string result, string1; s……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2245浏览 717个赞
C# 定义一个简单的事件(Event)public class MyClass : IDisposable{ public event EventHandler Disposing; public void Dispose() { // release any resources here if (D……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1836浏览 1526个赞
本范例演示了C#如何对一个给定的字符串进行MD5加密/// <summary>/// Calculates a MD5 hash from the given string and uses the given/// encoding./// </summary>/// <param name="Inpu……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2332浏览 1014个赞
C#提供了两种方式定义多行字符串变量string couplet1 = "Half a league, half a league,\n" + "Half a league onward,"; string couplet2 = @"All in the valley……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3086浏览 763个赞
C# 声明变量并赋值的代码演示class VariableDeclarations{ public static void Main() { // Type Identifier Assignment (maximum value) bool aBool; aBool = true; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2055浏览 1424个赞
本范例介绍了C#中const的用法和一些常量声明class ConstantDeclarations{ public static void Main() { // Type Identifier Initializer const bool aBool = true; co……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2308浏览 302个赞
本范例演示了C#开发GUI程序时如何避免一个超长处理过程占用全部资源,导致界面不能响应用户事件的方法// You need the Threading library to use the // "Thread.Sleep" function// using System.Threading; // This boolean……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1378浏览 2019个赞
本代码演示了如何创建一个unsafe的指针数组记录struct的地址信息class UnsafePointerArray{ public struct AStruct { public int anInteger; } public static void CreatePointerArray() { ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3006浏览 1393个赞