c# 笔记

Dictionary作为数据源

考虑到要同时绑定枚举成员的Text和Value,以及枚举成员与整型之间方便的进行转换,使用Dictionary<string,int>类型包装从给定的枚举类
型中读取的数据作为DropDownList的数据源。

private Dictionary<string, int> GetDataSource()
{
    Dictionary<string, int> retval = new Dictionary<string, int>();
    string[] enumNames = Enum.GetNames(m_enumType);
    for (int i = 0; i < enumNames.Length; i++)
    {
        retval.Add(enumNames[i], (int)Enum.Parse(m_enumType, enumNames[i]));

    }
    return retval;

}
 
//重写DataBind方法

public override void DataBind()
{
    base.DataSource = GetDataSource();
    DataValueField = "Value";
    DataTextField = "Key";
    base.DataBind();
 }

 读写txt文件

//1.添加命名空间

  System.IO;
  System.Text;

//2.文件的读取

 // (1).使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。

byte[] byData = new byte[100];
char[] charData = new char[1000];
public void Read()
{
try
{
FileStream file = new FileStream("E:\test.txt", FileMode.Open);
file.Seek(0, SeekOrigin.Begin);
file.Read(byData, 0, 100); //byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它

通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
Decoder d = Encoding.Default.GetDecoder();
d.GetChars(byData, 0, byData.Length, charData, 0);
Console.WriteLine(charData);
file.Close();
}
catch (IOException e)
{
Console.WriteLine(e.ToString());
}
}

  //(2).使用StreamReader读取文件,然后一行一行的输出。
public void Read(string path)
{
StreamReader sr = new StreamReader(path,Encoding.Default);
String line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line.ToString());
}
}
3.文件的写入
  (1).使用FileStream类创建文件,然后将数据写入到文件里。
public void Write()
{
FileStream fs = new FileStream("E:\ak.txt", FileMode.Create);
//获得字节数组
byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!");
//开始写入
fs.Write(data, 0, data.Length);
//清空缓冲区、关闭流
fs.Flush();
fs.Close();
}
  (2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。
public void Write(string path)
{
FileStream fs = new FileStream(path, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
//开始写入
sw.Write("Hello World!!!!");
//清空缓冲区
sw.Flush();
//关闭流
sw.Close();
fs.Close();
}

读写ini文件

{
publicstring path;
[DllImport("kernel32")]
privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);
[DllImport("kernel32")]
privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder
retVal,int size,string filePath);
public IniFile(string INIPath)
{
path = INIPath;
}
publicvoid IniWriteValue(string Section,string Key,string Value)
{
WritePrivateProfileString(Section,Key,Value,this.path);
}
publicstring IniReadValue(string Section,string Key)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);
return temp.ToString();
}
}

 Goto用法

using System;
class Hello
{
    public static void Main()
    {
        try
        {
            Console.WriteLine("开始执行try子句!");
            goto xx;
        }
        finally
        {
            Console.WriteLine("正在执行finally子句!");
        }
    xx:
        Console.WriteLine("正在执行xx标签!");
    }
}

 按位或,什么是按位异或,什么是按位与

& 按位与
| 按位或
^ 按位异或
1. 按位与运算
按位与运算符"&"是双目运算符。其功能是参与运算的两数各对应的二进位相与。只有对应的两个二进位均为1时,结果位才为1
,否则为0。参与运算的数以补码方式出现。
例如:9&5可写算式如下: 00001001 (9的二进制补码)&00000101
(5的二进制补码) 00000001 (1的二进制补码)可见9&5=1。
  按位与运算通常用来对某些位清0或保留某些位。例如把a
的高八位清 0 , 保留低八位, 可作 a&255 运算 ( 255
的二进制数为0000000011111111)。
main(){
int
a=9,b=5,c;
c=a&b;
printf("a=%d b=%d c=%d ",a,b,c);
}
2. 按位或运算
按位或运算符“|”是双目运算符。其功能是参与运算的两数各对应的二进位相或。只要对应的二个二进位有一个为1时,结果位就为1。参与运算的两个数均以补码出现。
例如:9|5可写算式如下:
00001001|00000101
00001101 (十进制为13)可见9|5=13
main(){
int
a=9,b=5,c;
c=a|b;
printf("a=%d b=%d c=%d ",a,b,c);
}
3. 按位异或运算
按位异或运算符“^”是双目运算符。其功能是参与运算的两数各对应的二进位相异或,当两对应的二进位相异时,结果为1。参与运算数仍以补码出现,例如9^5可写成算式如下:
00001001^00000101 00001100 (十进制为12)
main(){
int
a=9;
a=a^15;
printf("a=%d ",a);
}

实例演示操作:

位操作符是对数据按二进制位进行运算的操作符。位操作是其他很多语言都支持的操作,如C、C++和Java等,C#也不例外支持位操作。注意位操作支持的数据类型是基本数据类型,如byte、short、char、int、long等,C#支持的位操作有如下几种:
· 按位与

· 按位或 | 
· 按位取反 ~ 
· 左移 << 
· 右移
>>
· 异或^

在C#中位操作同C的位操作没有什么区别,位操作的速度相对较快,而且如果熟练的话,处理起来也相对方便,特别是在一些权限等相关的设置中,比如:用1、2、4、8、16、32、64分别代表查看、添加、编辑、修改、删除、审批等权限值的时候,如果某个用户的最终权限是多种权限值的叠加,用位操作来判断是否具有某种权限是相当方便的了。

using System;
/*
 * 作者:周公
 * 说明:本程序用以说明在C#中如何进行位操作。
 * 日期:2007-09-17
 * */
public class BitAction
{
    public static void Main(string[] args)
    {
        int[] power = new int[] { 1, 2, 4, 8, 16, 32, 64 };
        int value = 126;
        /*
         * 1的二进制形式:  00000001
         * 2的二进制形式:  00000010
         * 4的二进制形式:  00000100
         * 8的二进制形式:  00001000
         * 16的二进制形式: 00010000
         * 32的二进制形式: 00100000
         * 64的二进制形式: 01000000
         * 126的二进制形式:01111110
         */
        for (int i = 0; i < power.Length; i++)
        {
            if ((value & power[i]) != 0)
            {
                Console.WriteLine("有power[{0}]={1}所代表的权限", i, power[i]);
            }
        }
        Console.WriteLine("按位与:126&4={0}", value & 4);
        Console.WriteLine("按位或:126|4={0}", value | 4);
        Console.WriteLine("左移:126<<4={0}", value << 4);
        Console.WriteLine("右移:126>>4={0}", value & 4);
        Console.WriteLine("异或:126^4={0}", value ^ 4);
        Console.WriteLine("按位取反:~126={0}", ~value);
        Console.ReadLine();
    }
}
//举例说明
using System;
class MikeCat
{
public static void Main()
{
int a=6&3;
Console.WriteLine("a={0}",a);
//6的二进制是00000110,3的二进制是00000011,按位与后等于00000010,  即2。
    
int b=6|3;
Console.WriteLine("b={0}",b);
//6的二进制是00000110,3的二进制是00000011,按位或后等于00000111,即7

int c=~6;
Console.WriteLine("c={0}",c);
//6的二进制是00000110,按位取反后是11111001即-7

int d=6^3;
Console.WriteLine("d={0}",d);
//6的二进制是00000110,3的二进制是00000011,按位异或后等于00000101,即5

int e=6<<3;
Console.WriteLine("e={0}",e);
//6的二进制是00000110,左移三位后等于00101000,即48

int f=6>>2;
Console.WriteLine("f={0}",f);
//6的二进制是00000110,右移二位等于00000001,即1
  }
} 

 XML

     private static string _Store = LocalPathHelper.CurrentSolutionPath + "/data/bookstore.xml";
//1.添加节点

/// <summary>
/// 向根节点中插入一个节点
/// </summary>
public static void AddOne()
{
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(_Store);

    //1.查找booksotre节点
    XmlNode root = xmlDoc.SelectSingleNode("bookstore");
    //2.创建book 节点
    XmlElement book = xmlDoc.CreateElement("book");
    book.SetAttribute("genre", "lizanhong");
    book.SetAttribute("ISBN", "2-3431-4");
    XmlElement title = xmlDoc.CreateElement("title");
    title.InnerText = "C#入门经典";
    book.AppendChild(title);
    XmlElement author = xmlDoc.CreateElement("author");
    author.InnerText = "厚街";
    book.AppendChild(author);
    XmlElement price = xmlDoc.CreateElement("price");
    price.InnerText = "58.3";
    book.AppendChild(price);

    //将book节点,添加到根节点
    root.AppendChild(book);

    //保存内容
    xmlDoc.Save(_Store);
}


//2.修改节点

/// <summary>
/// 修改节点
/// </summary>
public static void UpdateOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //遍历修改
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        //将子节点类型转换为XmlEletment类型
        XmlElement ele = (XmlElement)node;
        if (ele.GetAttribute("genre") == "lizanhong")
        {
            ele.SetAttribute("genre", "udpate礼赞红");
            XmlNodeList nodeList2 = ele.ChildNodes;
            foreach (XmlNode node2 in nodeList2)
            {
                XmlElement ele2 = (XmlElement)node2;
                if (ele2.Name == "author")
                {
                    ele2.InnerText = "延纳";
                    break;
                }
            }
            break;
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 修改节点2,使用xpath
/// </summary>
public static void UpdateTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    //查询节点
    //XmlNode root = doc.SelectSingleNode("bookstore");
    //XmlNodeList books = doc.SelectNodes("bookstore/book");
    XmlNode title = doc.SelectNodes("bookstore/book/title")[0];
    title.InnerText = title.InnerText + "---xpath";
    doc.Save(_Store);
}
//3.删除节点

/// <summary>
/// 删除节点,属性,内容
/// </summary>
public static void DeleteOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);
    XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;
    foreach (var item in nodeList)
    {
        XmlElement ele = (XmlElement)item;
        if (ele.GetAttribute("genre") == "fantasy")
        {
            //删除属性
            ele.RemoveAttribute("genre");
        }
        else if (ele.GetAttribute("genre") == "udpate礼赞红")
        {
            //删除该节点的全部内容
            ele.RemoveAll();
        }
    }
    //保存修改
    doc.Save(_Store);
}
/// <summary>
/// 删除空节点
/// </summary>
public static void DeleteTwo()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (XmlNode node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        if (ele.ChildNodes.Count <= 0)
            //只能删除直接子节点
            root.RemoveChild(node);
    }
    doc.Save(_Store);
}
//4.查询列表

/// <summary>
/// 显示所有的数据
/// </summary>
public static void ShowOne()
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_Store);

    XmlNode root = doc.SelectSingleNode("bookstore");
    XmlNodeList nodeList = root.ChildNodes;
    foreach (var node in nodeList)
    {
        XmlElement ele = (XmlElement)node;
        Console.WriteLine(ele.GetAttribute("genre"));
        Console.WriteLine(ele.GetAttribute("ISBN"));
        XmlNodeList nodeList2 = ele.ChildNodes;
        foreach (XmlNode node2 in nodeList2)
        {
            Console.WriteLine(node2.InnerText);
        }
    }
}
原文地址:https://www.cnblogs.com/fer-team/p/8024138.html