C# 读写dat

 private static void WriteDat() {       
                //使用“另存为”对话框中输入的文件名实例化FileStream对象
                FileStream myStream = new FileStream("license.dat", FileMode.OpenOrCreate, FileAccess.ReadWrite);
                //使用FileStream对象实例化BinaryWriter二进制写入流对象
                BinaryWriter myWriter = new BinaryWriter(myStream);
                //以二进制方式向创建的文件中写入内容
                myWriter.Write("fdsfdsf");
                //关闭当前二进制写入流
                myWriter.Close();
                //关闭当前文件流
                myStream.Close();
        }
        private static string  ReadDat() {

            FileStream myStream = new FileStream("license.dat", FileMode.Open, FileAccess.Read);
            //使用FileStream对象实例化BinaryReader二进制写入流对象
            BinaryReader myReader = new BinaryReader(myStream);
            if (myReader.PeekChar() != -1)
            {
                //以二进制方式读取文件中的内容
                return Convert.ToString(myReader.ReadString());
            }
            //关闭当前二进制读取流
            myReader.Close();
            //关闭当前文件流
            myStream.Close();
            return null;
        }

 

你可能感兴趣的:(C#)