Пример #1
0
        public void writeFile(String fileName)
        {
            FileInfo fileInfo = new FileInfo(fileName);

            if (fileInfo.Extension == ".txt")
            {
                FileStream   fs = new FileStream(fileName, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs, Shape.TextEncodingType);
                shapeList.ForEach(delegate(Shape s)
                                  { s.writeText(sw); });
                sw.Close();
                fs.Close();
            }
            else if (fileInfo.Extension == ".bin")
            {
                FileStream   fs = new FileStream(fileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs, Shape.TextEncodingType);
                bw.Write(Shape.TextEncodingType.BodyName);

                shapeList.ForEach(delegate(Shape s)
                                  { s.writeBinary(bw); });
                bw.Close();
                fs.Close();
            }

            else if (fileInfo.Extension == ".ser")
            {
                FileStream fs = new FileStream(fileName, FileMode.Create);

                BinaryFormatter bf = new BinaryFormatter();
                try
                {
                    bf.Serialize(fs, shapeList);
                }
                catch (SerializationException e)
                {
                    Console.WriteLine("Failed to serialize. Reason: " + e.Message);
                    throw;
                }
                fs.Close();
            }
            else if (fileInfo.Extension == ".xml")
            {
                XDocument document = new XDocument();
                XElement  root     = new XElement("drawing");
                foreach (Shape shape in shapeList)
                {
                    root.Add(shape.createXmlElement());
                }
                document.Add(root);
                document.Save(fileName);
            }
            currentFile = fileName;
        }