Visual C# 通用语言示例主题(二)(MSDN整理)

15.代码:一次读取一行文本文件 (Visual C#)

本示例使用 StreamReader 类的 ReadLine 方法将文本文件的内容读取(一次读取一行)到字符串中。所有文本行都保存在字符串 line 中并显示在屏幕上。

示例:

int counter = 0;
string line;
// Read the file and display it line by line.
System.IO.StreamReader file =
   new System.IO.StreamReader("c://test.txt");
while((line = file.ReadLine()) != null)
{
   Console.WriteLine (line);
   counter++;
}
file.Close();
// Suspend the screen.
Console.ReadLine();

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

       ------》将 "c://test.txt" 替换为实际的文件名。

16.代码:从 XML 文件中读取类数据 (Visual C#)

本示例使用 XmlSerializer 类的 Deserialize 方法读取存储在 IntroToVCS.xml 文件中的对象上的数据。

示例:

public class Book
{
   public string title;
   static void Main()
   {
      Book introToVCS  = new Book();
      System.Xml.Serialization.XmlSerializer reader = new
         System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
      // Read the XML file.
      System.IO.StreamReader file=
         new System.IO.StreamReader("c://IntroToVCS.xml");
      // Deserialize the content of the file into a Book object.
      introToVCS = (Book) reader.Deserialize(file);
      System.Windows.Forms.MessageBox.Show(introToVCS.title,
         "Book Title");
   }
}

编译代码-----》可以直接使用命令行编译该示例,也可以使用 Visual Studio IDE 将代码粘贴到控制台应用程序中。在后一种情况下,您必须引用 System.Windows.Forms.dll 文件。

       ------》将 "c://IntroToVCS.xml" 替换为实际的文件名。

17.代码:从文本文件中读取 (Visual C#)

本示例使用 StreamReader 类的 ReadToEnd 方法将文本文件的内容读入到字符串中。

示例:

// Read the file as one string.
System.IO.StreamReader myFile =
   new System.IO.StreamReader("c://test.txt");
string myString = myFile.ReadToEnd();
myFile.Close();
// Display the file contents.
Console.WriteLine(myString);
// Suspend the screen.
Console.ReadLine();

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        -----》将 "c://test.txt" 替换为实际的文件名。

18.代码:从文件中读取 XML (Visual C#)

本示例使用 XMLTextReader 类从 IntroToVCS.xml 文件提取元素名称和文本字符串并将该信息存储在字符串变量中。

示例:

System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader("c://IntroToVCS.xml");
         string contents = "";
         while (reader.Read())
         {

            reader.MoveToContent();
            if (reader.NodeType == System.Xml.XmlNodeType.Element)
               contents += "<"+reader.Name + ">/n";
            if (reader.NodeType == System.Xml.XmlNodeType.Text)
               contents += reader.Value + "/n";
         }
Console.Write(contents);

编译代码------》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        ------》将 "c://IntroToVCS.xml" 替换为实际的文件名。

19.代码:运行程序 (Visual C#)

本示例使用 Process.Start 方法加载并运行 MSPaint 应用程序。

示例:

System.Diagnostics.Process.Start("mspaint.exe");

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        -----》将 "mspaint.exe" 替换为想要运行的应用程序的路径。

20.代码:运行与文件类型相关联的程序 (Visual C#)

本示例使用 Process.Start 方法在记事本中打开 test.txt 文件。

示例:

System.Diagnostics.Process.Start("c://test.txt");

编译代码------》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

        ------》将“c://test.txt”文件替换为想要打开的文件。

21.代码:在字符串数组中查找一个字符串 (Visual C#) 

本示例在字符串数组上调用 IndexOf 方法报告字符串数目和子字符串第一个匹配项的索引。

示例:
string[] strArray = {"ABCDEFG", "HIJKLMNOP"};
string findThisString = "JKL";
int strNumber;
int strIndex = 0;
for (strNumber = 0; strNumber < strArray.Length; strNumber++)
{
    strIndex = strArray[strNumber].IndexOf(findThisString);
    if (strIndex >= 0)
        break;
}
Console.WriteLine("String number: {0}/nString index: {1}",
    strNumber, strIndex);
编译代码------》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
可靠编程------》 IndexOf 方法报告子字符串第一个匹配项的第一个字符的位置。该索引是从 0 开始的,这意味着字符串第一个字符的索引为 0。

       ------》如果 IndexOf 没有找到该子字符串,则返回 -1。

       -----》IndexOf 方法区分大小写,并使用当前区域性。

22.代码:在字符串中进行搜索 (Visual C#)

本示例对 String 对象调用 IndexOf 方法,以报告子字符串的第一个匹配项的索引。
示例:
string searchWithinThis = "ABCDEFGHIJKLMNOP";
string searchForThis = "DEF";
int firstCharacter = searchWithinThis.IndexOf(searchForThis);
Console.WriteLine("First occurrence: {0}", firstCharacter);
编译代码------》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
可靠编程------》同上。
23.代码:设置对象上的属性 (Visual C#)
本示例将 Environment 类上的 CurrentDirectory 属性设置为 C:/Public。
示例:
Environment.CurrentDirectory = "C://Public"; 或
Environment.CurrentDirectory = @"C:/Public";
编译代码------》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
24.代码:模拟默认参数 (Visual C#)
本示例说明如何使用方法重载模拟默认参数,这些参数在 C# 中是不允许的。
示例:
class MyClass
{
    static string myMethod(string precip, string country, string location)
    {
        return string.Format("The {0} in {1} stays mainly in the {2}.",
            precip, country, location );
    }
    static string myMethod(string precip, string country )
    {
        return myMethod(precip, country, "plain");
    }
    static string myMethod()
    {
        return myMethod("rain", "Spain", "plain");
    }
    static void Main(string[] args)
    {
        Console.WriteLine(myMethod());
        Console.WriteLine(myMethod("snow", "Walla Walla"));
    }
}
编译代码-----》复制该类并将其粘贴在控制台应用程序中的 Class1 上。
25.代码:引发异常 (Visual C#)
本示例将引发 System.ApplicationException 异常。
示例:
throw new ApplicationException();
编译代码----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。
26.代码:使用 PInvoke (Visual C#)
本示例显示如何使用 DllImport 属性通过从 user32.dll 调用 MessageBoxA 创建消息框。
示例:
class PlatformInvokeTest
{
   [DllImport("user32.dll")]
   public static extern int MessageBoxA(
      int h, string m, string c, int type);
   public static int Main()
   {
      return MessageBoxA(0, "Hello World!", "My Message Box", 0);
   }
}
编译代码-----》复制该类并将其粘贴在控制台应用程序中的 Class1 上。

       ------》将 using 指令添加至 System.Runtime.InteropServices 命名空间。 

可靠编程-----》确保含有所需方法的 DLL(此例中为 user32.dll)名称正确。

       ------》确保使用 MessageBoxA 函数的适当参数。

27.代码:将类数据写入 XML 文件 (Visual C#)

本示例使用 XmlSerializer 类的 Serialize 方法将对象中存储的数据写入 IntroToVCS.xml 中。

示例:

public class Book
{
   public string title;
   static void Main()
   {
      Book introToVCS = new Book();
      introToVCS.title = "Intro to Visual CSharp";
      System.Xml.Serialization.XmlSerializer writer =
         new System.Xml.Serialization.XmlSerializer(introToVCS.GetType());
      System.IO.StreamWriter file =
         new System.IO.StreamWriter("c://IntroToVCS.xml");
      writer.Serialize(file, introToVCS);
      file.Close();
   }
}

编译代码-----》可以直接使用命令行编译该示例,也可以使用 Visual Studio IDE 将代码粘贴到控制台应用程序中。

       ------》将 "c://IntroToVCS.xml" 替换为实际的文件名。

28.代码:写入文本文件 (Visual C#)

本示例使用 StreamWriter 类的 WriteLine 方法将字符串写入到文本文件中。

示例:

// Compose a string that consists of three lines.
string lines = "First line./r/nSecond line./r/nThird line.";
// Write the string to a file.
System.IO.StreamWriter file = new System.IO.StreamWriter("c://test.txt");

file.WriteLine(lines);
file.Close();

编译代码-----》复制该代码,并将其粘贴到控制台应用程序的 Main 方法中。

       ------》将 "c://test.txt" 替换为实际的文件名。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值