C#構文ベース-05-ファイル操作
C#構文ベース-05-ファイル操作
この二つの文章を書く目的は、C#言語は大学で勉強したことがあり、当時何かをしたことがありますが、主な仕事はC#とは関係なく忘れてしまいました.最近、会社はUnity業務を増やし、Unityを書くのは主にC#とjsであり、C#の文法構造はJavaとよく似ていると考え、C#言語を会社のゲームプロジェクトの主な言語として採用した.
このシリーズは主に上下の3つの文章に分けて記録されている.C#の初級、中級、高級内容にそれぞれ関与している.
今月はずっと会社のプロジェクトで忙しかったので、メールが遅れてしまいましたが、五月が忙しくなったことを思い出して、整理して送りましょう.
本文は主にC#言語の高級知識について書いて、もし初級のを見たことがないならば、まず1篇の文章を見て、コンピュータの上でたたいてみて、走ってみてください.ファイルアクション_ファイルおよびフォルダ情報の表示
2.Fileを使用してファイルを読み書きする FileStreamを使用してファイルを読み書きする StreamReaderおよびStreamWriter xml読み書き json操作
7.excel操作まとめ やはりその言叶、多く考えて、コードを上手に叩いて、デバッグして、多く试してみます.問題があれば、コメントを残して勉強して進歩することができます.
この二つの文章を書く目的は、C#言語は大学で勉強したことがあり、当時何かをしたことがありますが、主な仕事はC#とは関係なく忘れてしまいました.最近、会社はUnity業務を増やし、Unityを書くのは主にC#とjsであり、C#の文法構造はJavaとよく似ていると考え、C#言語を会社のゲームプロジェクトの主な言語として採用した.
このシリーズは主に上下の3つの文章に分けて記録されている.C#の初級、中級、高級内容にそれぞれ関与している.
今月はずっと会社のプロジェクトで忙しかったので、メールが遅れてしまいましたが、五月が忙しくなったことを思い出して、整理して送りましょう.
本文は主にC#言語の高級知識について書いて、もし初級のを見たことがないならば、まず1篇の文章を見て、コンピュータの上でたたいてみて、走ってみてください.
namespace _ _ {
class Program {
static void Main(string[] args) {
// :
//FileInfo fileInfo = new FileInfo("TextFile1.txt");
// :
//FileInfo fileInfo = new FileInfo(@"C:\Users\samuelnotes\ csharp \01- - \bin\Debug\TextFile1.txt");
//Console.WriteLine(fileInfo.Exists);//
//Console.WriteLine(fileInfo.Name);// .
//Console.WriteLine(fileInfo.Directory);//
//Console.WriteLine(fileInfo.Length);
//Console.WriteLine(fileInfo.IsReadOnly);
////fileInfo.Delete();// ,
//fileInfo.CopyTo("tt.txt");
//FileInfo fileInfo = new FileInfo("siki.txt");
//if (fileInfo.Exists == false)//
//{
// fileInfo.Create();//
//}
//fileInfo.MoveTo("siki2.txt");//
// ( ) ( )
//DirectoryInfo dirInfo = new DirectoryInfo(@"C:\Users\samuelnotes\Documents\01- - \bin\Debug");// Debug
//Console.WriteLine(dirInfo.Exists);
//Console.WriteLine(dirInfo.Name);
//Console.WriteLine(dirInfo.Parent);
//Console.WriteLine(dirInfo.Root);
//Console.WriteLine(dirInfo.CreationTime);
//dirInfo.CreateSubdirectory("samuelnotes");
//DirectoryInfo dirInfo = new DirectoryInfo("test");
//if (dirInfo.Exists == false)
//{
// dirInfo.Create();//
//}
Console.ReadKey();
}
}
}
2.Fileを使用してファイルを読み書きする
namespace _02_ File {
class Program {
static void Main(string[] args)
{
//string[] strArray = File.ReadAllLines("TextFile1.txt");// , ,
//foreach (var s in strArray)
//{
// Console.WriteLine(s);
//}
//string s = File.ReadAllText("TextFile1.txt");
//Console.WriteLine(s);
//byte[] byteArray = File.ReadAllBytes("3.LINQ.png");
//foreach (var b in byteArray)
//{
// Console.Write(b);
//}
//File.WriteAllText("textfile2.txt", "hello sdf ");
//File.WriteAllLines("textfile3.txt",new string[]{" sdfsdflk","213412"," "});
byte[] data = File.ReadAllBytes("3.LINQ.png");
File.WriteAllBytes("4.png",data);
Console.ReadKey();
}
}
}
namespace _03_ FileStream {
class Program {
static void Main(string[] args) {
//1,
//FileStream stream = new FileStream("TextFile1.txt",FileMode.Open);
////2,
//byte[] data = new byte[1024];//
////1 byte = 1 1024byte=1KB 1024KB=1MB 1024MB = 1GB 1024GB=1T
//while (true)
//{
// int length = stream.Read(data, 0, data.Length);
// if (length == 0)
// {
// Console.WriteLine(" ");
// break;
// }
// for (int i = 0; i < length; i++) {
// Console.Write(data[i] + " ");
// }
//}
// filestream
// 1.5MB = 1.5*1024KB = 2k KB= 2k *1000 byte
FileStream readStream = new FileStream("3.LINQ.png",FileMode.Open);
FileStream writeStream = new FileStream("LINQ .png",FileMode.Create);
byte[] data = new byte[1024];
while (true)
{
int length = readStream.Read(data, 0, data.Length);
if (length == 0)
{
Console.WriteLine(" ");
break;
}
else
{
writeStream.Write(data,0,length);
}
}
writeStream.Close();
readStream.Close();
Console.ReadKey();
}
}
}
namespace _04_ StreamReader StreamWriter {
class Program {
static void Main(string[] args) {
//
//StreamReader reader = new StreamReader("TextFile1.txt");
//while (true)
//{
// string str = reader.ReadLine();//
// if (str == null) break;
// Console.WriteLine(str);
//}
//string str = reader.ReadToEnd();// ( )
//Console.WriteLine(str);
//while (true)
//{
// int res = reader.Read();//
// if (res == -1)
// {
// break;
// }
// else
// {
// Console.Write((char)res);
// }
//}
//reader.Close();
//
StreamWriter writer = new StreamWriter("textfile2.txt");// ,
while (true)
{
string message = Console.ReadLine();
if (message == "q")
break;
//writer.Write(message);//
writer.WriteLine(message);//
}
writer.Close();
//Console.ReadKey();
}
}
}
namespace _5_xml {
class Program {
static void Main(string[] args) {
// ,
List skillList = new List();
// XmlDocment xml
XmlDocument xmlDoc = new XmlDocument();
// xml
//xmlDoc.Load("skillinfo.txt");
xmlDoc.LoadXml( File.ReadAllText("skillinfo.txt") );// (xml )
// (xmlnode )
XmlNode rootNode = xmlDoc.FirstChild;//
//
XmlNodeList skillNodeList= rootNode.ChildNodes;//
foreach (XmlNode skillNode in skillNodeList)
{
Skill skill = new Skill();
XmlNodeList fieldNodeList = skillNode.ChildNodes;// skill
foreach (XmlNode fieldNode in fieldNodeList)
{
if (fieldNode.Name == "id")// Name
{
int id = Int32.Parse(fieldNode.InnerText);//
skill.Id = id;
}else if (fieldNode.Name == "name")
{
string name = fieldNode.InnerText;
skill.Name = name;
skill.Lang = fieldNode.Attributes[0].Value;
}
else
{
skill.Damage = Int32.Parse(fieldNode.InnerText);
}
}
skillList.Add(skill);
}
foreach (Skill skill in skillList)
{
Console.WriteLine(skill);
}
Console.ReadKey();
}
}
}
class Skill {
public int Id { get; set; }
public string Name { get; set; }
public string Lang { get; set; }
public int Damage { get; set; }
public override string ToString()
{
return string.Format("Id: {0}, Name: {1}, Lang: {2}, Damage: {3}", Id, Name, Lang, Damage);
}
}
namespace _xml _ {
class Program {
static void Main(string[] args) {
List skillList = new List();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("xml .txt");
//XmlNode skillInfoNode = xmlDoc.FirstChild;
//XmlNode skillListNode = skillInfoNode.FirstChild;
XmlNode skillListNode = xmlDoc.FirstChild.FirstChild;
XmlNodeList skillNodeList = skillListNode.ChildNodes;
foreach (XmlNode skillNode in skillNodeList)
{
Skill skill = new Skill();
XmlElement ele = skillNode["Name"];
skill.Name = ele.InnerText;
XmlAttributeCollection col = skillNode.Attributes;//
//skill.Id = Int32.Parse(col["SkillID"].Value);
XmlAttribute idAttribute = col["SkillID"];//
skill.Id = Int32.Parse(idAttribute.Value);
skill.EngName = col["SkillEngName"].Value;
skill.TriggerType = Int32.Parse(col["TriggerType"].Value) ;
skill.ImageFile = col["ImageFile"].Value;
skill.AvailableRace = Int32.Parse(col["AvailableRace"].Value);
skillList.Add(skill);
}
foreach (Skill s in skillList)
{
Console.WriteLine(s);
}
Console.ReadKey();
}
}
}
namespace _06_json {
class Program {
static void Main(string[] args) {
// litjson json
// litjson
//1, litjson litjson.dll dll
//2, netget , litjson
//List skillList = new List();
// jsonMapper json
//jsondata
// jsonData
//JsonData jsonData = JsonMapper.ToObject(File.ReadAllText("json .txt"));
//foreach (JsonData temp in jsonData)// temp
//{
// Skill skill = new Skill();
// JsonData idValue =temp["id"]; //
// JsonData nameValue = temp["name"];
// JsonData damageValue = temp["damage"];
// int id = Int32.Parse(idValue.ToString());
// int damage = Int32.Parse(damageValue.ToString());
// //Console.WriteLine(id+":"+nameValue.ToString()+":"+damage);
// skill.id = id;
// skill.damage = damage;
// skill.name = nameValue.ToString();
// skillList.Add(skill);
//}
//foreach (var temp in skillList)
//{
// Console.WriteLine(temp);
//}
// json
//json
//Skill[] skillArray= JsonMapper.ToObject(File.ReadAllText("json .txt"));
//foreach (var temp in skillArray)
//{
// Console.WriteLine(temp);
//}
//List skillList = JsonMapper.ToObject>(File.ReadAllText("json .txt"));
//foreach (var temp in skillList) {
// Console.WriteLine(temp);
//}
//Player p= JsonMapper.ToObject(File.ReadAllText(" .txt"));
//Console.WriteLine(p);
//foreach (var temp in p.SkillList)
//{
// Console.WriteLine(temp);
//}
Player p = new Player();
p.Name = " ";
p.Level = 100;
p.Age = 16;
string json =JsonMapper.ToJson(p);
Console.WriteLine(json);
Console.ReadKey();
}
}
}
class Player
{
// public string name;// json
// public int level;
public string Name { get; set; }
public int Level { get; set; }
public int Age { get; set; }
public List SkillList { get; set; }
public override string ToString()
{
return string.Format("Name: {0}, Level: {1}, Age: {2}, SkillList: {3}", Name, Level, Age, SkillList);
}
}
class Skill
{
public int id;
public int damage;
public string name;
public override string ToString()
{
return string.Format("Id: {0}, Damage: {1}, Name: {2}", id, damage, name);
}
}
7.excel操作
namespace _07_Excel {
class Program {
static void Main(string[] args)
{
string fileName = " .xls";
string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileName + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
//
OleDbConnection connection = new OleDbConnection(connectionString);
//
connection.Open();
string sql = "select * from [Sheet1$]";//
OleDbDataAdapter adapter = new OleDbDataAdapter(sql,connection);
DataSet dataSet = new DataSet();// DataTable
adapter.Fill(dataSet);// (datatable) ( )dataset
connection.Close();//
//
DataTableCollection tableCollection= dataSet.Tables;//
DataTable table = tableCollection[0];// dataset , 0
//
// table
DataRowCollection rowCollection = table.Rows;//
// , datarow
foreach (DataRow row in rowCollection)
{
// row 8 0-7
for (int i = 0; i < 8; i++)
{
Console.Write(row[i]+" ");
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}