这段代码可以抓取指定的url的网络图片,并保存到本地 public Bitmap Get_img() { Bitmap img = null; HttpWebRequest req; HttpWebResponse res = null; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2051浏览 1811个赞
C#中计算字符串长度,一个中文算两个字符int len1 = System.Text.Encoding.Default.GetBytes("aaa").Length; //结果为 3int len2 = System.Text.Encoding.Default.GetBytes("张三丰").Length; /……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2934浏览 1486个赞
这段代码从控制台接收一个字符串,然后判断该字符串是否可以被转换成整型,用到int.TryParsestring i = Console.ReadLine(); int a=0; if (int.TryParse(i, out a) == false) //判断是否可以转换为整型 {……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1477浏览 105个赞
这段代码是一个IIS操作类,可以最IIS进行站点的添加、删除、修改等操作//IISHelper.csusing System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Text.RegularExpressions;us……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2728浏览 1387个赞
C# 获得当前运行程序所在的目录using System;using System.IO; class DirectoryCurrent{ public static void Main() { Console.WriteLine ("Current directory: \"{0}\""……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2716浏览 615个赞
C# 获得目录创建时间,在这段代码中先创建一个目录,获取创建时间后,将目录删除using System;using System.IO; class DirectoryCreationTime{ public static void Main() { string directoryString = ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1257浏览 2002个赞
C#创建和删除目录using System;using System.IO; class DirectoryCreation{ public static void Main() { string directoryString = Directory.GetCurrentDirectory() +……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2335浏览 2392个赞
C#清除IE浏览器缓存,IE的临时文件夹using System.IO; ...void clearIECache(){ ClearFolder (new DirectoryInfo (Environment.GetFolderPath (Environment.SpecialFolder.InternetCache)));}……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2300浏览 2210个赞
C# 创建Group Object in Active Directoryusing System.DirectoryServices;using System.Reflection; public class ADGroup{ private String ADGRPOUP = "group"; // ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2930浏览 2763个赞
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) 2366浏览 1633个赞
C#检测网络驱动器的剩余磁盘空间using System;using System.Management; class NetworkSpace{ static void Main(string[] args) { SelectQuery query = new SelectQuery( "select……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1216浏览 698个赞
C#检测光驱里的光盘是否已经加载using System;using System.Management; class App{ public static void Main() { SelectQuery query = new SelectQuery( "select * from win32_logicaldi……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2813浏览 2436个赞
这段代码演示了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) 1901浏览 2179个赞
C# 检测pc光驱里是否插入了光盘,需要添加System.Management.dll 的引用using System;using System.Management; namespace CDROMManagement{ class WMIEvent { static void Main(string[] args) ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1358浏览 286个赞
本代码创建了一个类的索引器用来访问类,就像访问数组一样using System.Collections.Generic;...class Client{ private Dictionary<int, string> invoices = new Dictionary<int, string>(); ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2518浏览 1837个赞
本代码通过ICollection接口的count属性来输出其元素数量using System;using System.Collections; public class CountArray{ public static void Main() { string[] strings = {"Ajax&quo……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1869浏览 782个赞
这段代码通过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) 2623浏览 1984个赞
本范例演示了C#中如何在不打开dos窗口的情况下运行控制台程序,同时捕获程序的输出信息// This snippet needs the "System.Diagnostics"// library // Application path and command line argumentsstring Applicatio……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3021浏览 1274个赞
C#枚举类型用于标志位,可以进行与和或操作enum Mammalia{ Bison = 1, Human = 2, Mammal = 255} public class MammalCheck{ public static void Main( ) { //Mammalia pattern……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2424浏览 799个赞
本代码演示了C#自带的Array.Sort方法排序using System; public class ReverseArraySort{ public static void Main() { string[] strings = {"beta", "alpha", "……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2436浏览 1237个赞
这段代码演示了C#使用二分法快速搜索数组using System; class ArrayBinarySearch{ public static void Main() { int[] ints = {0, 10, 100, 1000, 1000000 }; Console.WriteLine ("A……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3089浏览 2882个赞
这段代码可以把C#的数组倒置过来,主要用到了Array.Reverse方法using System; public class ReverseArray{ public static void Main() { string[] strings = {"beta", "alpha"……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1646浏览 2717个赞
C# 读取控制台输入的信息char character = (char) Console.Read(); // returns an int string stringInput = Console.ReadLine();……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2109浏览 687个赞
C# 给函数传入匿名委托并控制执行指定的次数public delegate int ConsoleDelegate (int value); public class Test{ void ExecuteDelegate (ConsoleDelegate cd, int iterations) { for (int i=1……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1710浏览 545个赞
C# 重写ToString()方法输出私有变量的代码片段public class MyClass{ private string customer =""; private int customerID = 0; public string Customer { get { return c……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1478浏览 297个赞
C# 重载加号运算符计算两个对象的和public class ValidFloat{ private float value; private bool valid; public ValidFloat (float value, bool valid) { this.value = value; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2436浏览 569个赞
模仿老版本的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) 2602浏览 1233个赞
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) 3087浏览 2557个赞
本代码片段演示了C#中如何实现IDispose接口,实现资源释放public class MyClass : IDisposable{ public void Dispose() { // release any resources here // when object is about to be garb……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3371浏览 2238个赞
C#获取 NetBIOS和 DNS计算机名static string GetLocalHostName (){ string netBiosName = System.Environment.MachineName; //return netBiosName; // Following method is deprecated……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2524浏览 248个赞
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) 1507浏览 639个赞
C# 从控制台读取数字try{ int number = Convert.ToInt32 (Console.ReadLine()); Console.WriteLine ("Number: " + number);}catch (FormatException e){ Console.WriteLine (……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1449浏览 2466个赞
下面的c#代码片段显示产生随机数输出到控制台。RandomNumber函数的第一个参数为生成的随机数的最小值。第二个参数是最大值+ 1。using System; class Program{ private Random random = new Random(); private int RandomNumber (int ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1849浏览 2038个赞
根据给定的网页url地址返回域名/* ** Method 1 (using the build-in Uri-object)*/public static string ExtractDomainNameFromURL_Method1(string Url){ if (!Url.Contains("://")) ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1567浏览 978个赞
下面的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) 1658浏览 1433个赞
下面的c#代码片段演示了各种字符串的方法和属性。using System; class StringMethods{ static void Main() { char[] characterArray; int position; string result, string1; s……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1609浏览 1698个赞
C# 定义一个简单的事件(Event)public class MyClass : IDisposable{ public event EventHandler Disposing; public void Dispose() { // release any resources here if (D……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2160浏览 810个赞
本范例演示了C#如何对一个给定的字符串进行MD5加密/// <summary>/// Calculates a MD5 hash from the given string and uses the given/// encoding./// </summary>/// <param name="Inpu……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2795浏览 2793个赞
C#提供了两种方式定义多行字符串变量string couplet1 = "Half a league, half a league,\n" + "Half a league onward,"; string couplet2 = @"All in the valley……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1775浏览 560个赞
C# 声明变量并赋值的代码演示class VariableDeclarations{ public static void Main() { // Type Identifier Assignment (maximum value) bool aBool; aBool = true; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2412浏览 738个赞
本范例介绍了C#中const的用法和一些常量声明class ConstantDeclarations{ public static void Main() { // Type Identifier Initializer const bool aBool = true; co……继续阅读 » 水墨上仙 4年前 (2021-03-14) 2114浏览 2813个赞
本范例演示了C#开发GUI程序时如何避免一个超长处理过程占用全部资源,导致界面不能响应用户事件的方法// You need the Threading library to use the // "Thread.Sleep" function// using System.Threading; // This boolean……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3064浏览 1494个赞
本代码演示了如何创建一个unsafe的指针数组记录struct的地址信息class UnsafePointerArray{ public struct AStruct { public int anInteger; } public static void CreatePointerArray() { ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3133浏览 2056个赞
本范例演示怎样通过c#中的XmlSerializer序列化一个普通对象// This is the test class we want to // serialize:[Serializable()]public class TestClass{ private string someString; public string……继续阅读 » 水墨上仙 4年前 (2021-03-14) 3064浏览 1796个赞
C#创建多维数组代码using System; public class MultidimensionalArray{ public static void Main() { int[] lowerBounds = {1, 2, 4}; int[] lengths = {4, 2, 1}; ……继续阅读 » 水墨上仙 4年前 (2021-03-14) 1920浏览 1543个赞