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 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);
------》如果 IndexOf 没有找到该子字符串,则返回 -1。
-----》IndexOf 方法区分大小写,并使用当前区域性。
22.代码:在字符串中进行搜索 (Visual C#)
string searchForThis = "DEF";
int firstCharacter = searchWithinThis.IndexOf(searchForThis);
Console.WriteLine("First occurrence: {0}", firstCharacter);
{
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
上。
{
[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"
替换为实际的文件名。