class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists. if (!File.Exists(path)) { // Create the file. using (FileStream fs = File.Create(path)) { Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file. fs.Write(info, 0, info.Length); } } // Open the stream and read it back. using (StreamReader sr = File.OpenText(path)) { string s = ""; while ((s = sr.ReadLine()) != null) { ComboBox.Items.Add(s); } } } }
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt"; // Delete the file if it exists.
if (!File.Exists(path))
{
// Create the file.
using (FileStream fs = File.Create(path))
{
Byte[] info =
new UTF8Encoding(true).GetBytes("This is some text in the file."); // Add some information to the file.
fs.Write(info, 0, info.Length);
}
} // Open the stream and read it back.
using (StreamReader sr = File.OpenText(path))
{
string s = "";
while ((s = sr.ReadLine()) != null)
{
ComboBox.Items.Add(s);
}
}
}
}
System.Data.DataTable dtbl = .... //从数据库中检索的数据表
dtbl.WriteXml("dataFile.xml");//读出,并绑定到 comboBox
System.Data.DataTable dtbl = new System.Data.DataTable();
dtbl.ReadXml("dataFile.xml");
this.comboBox1.DataSource = dtbl;
/// 从文本文件中读数据,返回一个数组
/// </summary>
public static string[] TextData
{
get
{
string[] result = HttpContext.Current.Cache["Ustbwuyi_ReadTextData_1.0"] as string[];
if (result == null)
{
List<string> list = new List<string>();
using(StreamReader sr=new StreamReader(HttpContext.Current.Server.MapPath("~/App_Data/Provider.txt"),Encoding.UTF8))
{
string str = "";
while ((str = sr.ReadLine()) != null)
{
list.Add(str);
}
result = list.ToArray();
HttpContext.Current.Cache.Insert("Ustbwuyi_ReadTextData_1.0", result, new System.Web.Caching.CacheDependency(HttpContext.Current.Server.MapPath("~/App_Data/Provider.txt")));
}
}
return result;
}
}