您的位置:首页 > 编程语言 > C#

C#_File_Assembly

2015-09-24 08:38 302 查看
//--------------------------------------------^ ^-----------------------------------------------------------

static void Main(string[] args)

{

  //1.get current assembly location

  string currentPath=Assembly.GetExecutingAssembly().Location;    //Get Executing Assembly()  

  string currentDirectoryPath=Path.GetDirectoryName(currentPath);

  

  //2.create a file under current directory path

  FileStream fs=File.Create(currentDirectoryPath+@"\test.txt");

  fs.Close();

  

  string msg="This is a test string..";

  //3.change string to byte[]

  byte[] bs=System.Text.Encoding.UTF-8.GetBytes(msg);

  File.WriteAllBytes(currentDirectoryPath+@"\test.txt",bs);

  

  //4.change byte[] to string

  byte[] rbs=File.ReadAllBytes(currentDirectoryPath+@"\test.txt");

  string rString=System.Text.Encoding.UTF-8.GetString(rbs);

  Console.WriteLine(rString);

}

//-----------------------------------------------------------------------------------------

编码乱码问题:

#region 工资读写Exercise
using(StreamReader sReader=new StreamReader("工资文件.txt",Encoding.Default))
{
using (StreamWriter sWriter = new StreamWriter("new.txt", false))
{
while (!sReader.EndOfStream)
{
string line = sReader.ReadLine();
string[] lineparts = line.Split(new char[] { '|'});
int doubleSalary = Convert.ToInt32(lineparts[1])*2;
lineparts[1] = doubleSalary.ToString();
string newline = lineparts[0] + "|" + lineparts[1];
sWriter.WriteLine(newline);
}
}
}
#endregion


ReaderAndWriter
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: