Пример #1
0
        /// <summary>
        /// Saves obj to DB and file. Obj MUST have its cid set to a valid value.
        /// </summary>
        /// <param name="obj">Object to save</param>
        private eObject saveObject(CreatorObject obj)
        {
            string originalSoundFileName = "";

            byte[] data = null;

            if (obj.cid == -1)
            {
                throw new Exception("Trying to save object without first setting cid");
            }

            // Kill off the old bmp file so it doesnt hog the file
            // while we try to delete and overwrite
            if (obj.type == Constant.imageFile)
            {
                Image originalBmp = obj.imageBox.BackgroundImage;
                obj.imageBox.BackgroundImage = new Bitmap(obj.imageBox.BackgroundImage);
                originalBmp.Dispose();
            }
            else if (obj.type == Constant.soundFile)
            {
                originalSoundFileName = obj.playerControl.filePath;
                obj.playerControl.close();

                FileStream userFile = new FileStream(originalSoundFileName, FileMode.Open, FileAccess.Read);
                data = new byte[userFile.Length];
                userFile.Read(data, 0, Convert.ToInt32(userFile.Length));
                userFile.Close();
            }

            // Delete existing file
            FileInfo file = new FileInfo(Constant.ePath + obj.eObj.actualFilename);

            if (file.Exists)
            {
                try
                {
                    file.Delete();
                }
                catch (Exception e)
                {
                    MessageBox.Show("Unable to write to file. It is being used by another application.\n\n" + e.ToString(), "File I/O error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }

            eObject eObj = obj.toEObject();

            // Insert into DB
            dbAccess.insertLocalDB.insertToObjects(eObj);

            // Save file
            FileStream fs = new FileStream(Constant.ePath + eObj.actualFilename, FileMode.Create, FileAccess.ReadWrite);

            switch (eObj.type)
            {
            case Constant.textFile:
                obj.textbox.SaveFile(fs, RichTextBoxStreamType.RichText);
                break;

            case Constant.imageFile:
                if (obj.imageBox.BackgroundImage != null && obj.imageBox.BackgroundImage != global::eFlash.Properties.Resources.defaultImage)
                {
                    obj.imageBox.BackgroundImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                break;

            case Constant.soundFile:
                fs.Write(data, 0, data.Length);
                obj.playerControl.setFile(Constant.ePath + eObj.actualFilename);
                break;

            default:
                throw new Exception("Invalid object type");
            }
            fs.Flush();
            fs.Close();
            fs.Dispose();

            return(eObj);
        }
Пример #2
0
        /// <summary>
        /// Saves obj to DB and file. Obj MUST have its cid set to a valid value. 
        /// </summary>
        /// <param name="obj">Object to save</param>
        private eObject saveObject(CreatorObject obj)
        {
            string originalSoundFileName = "";
            byte[] data = null;

            if (obj.cid == -1)
            {
                throw new Exception("Trying to save object without first setting cid");
            }

            // Kill off the old bmp file so it doesnt hog the file
            // while we try to delete and overwrite
            if (obj.type == Constant.imageFile)
            {
                Image originalBmp = obj.imageBox.BackgroundImage;
                obj.imageBox.BackgroundImage = new Bitmap(obj.imageBox.BackgroundImage);
                originalBmp.Dispose();
            }
            else if (obj.type == Constant.soundFile)
            {
                originalSoundFileName = obj.playerControl.filePath;
                obj.playerControl.close();

                FileStream userFile = new FileStream(originalSoundFileName, FileMode.Open, FileAccess.Read);
                data = new byte[userFile.Length];
                userFile.Read(data, 0, Convert.ToInt32(userFile.Length));
                userFile.Close();
            }

            // Delete existing file
            FileInfo file = new FileInfo(Constant.ePath + obj.eObj.actualFilename);
            if (file.Exists)
            {
                try
                {
                    file.Delete();
                }
                catch (Exception e)
                {
                    MessageBox.Show("Unable to write to file. It is being used by another application.\n\n" + e.ToString(), "File I/O error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    this.Close();
                }
            }

            eObject eObj = obj.toEObject();

            // Insert into DB
            dbAccess.insertLocalDB.insertToObjects(eObj);

            // Save file
            FileStream fs = new FileStream(Constant.ePath + eObj.actualFilename, FileMode.Create, FileAccess.ReadWrite);
            switch (eObj.type)
            {
                case Constant.textFile:
                    obj.textbox.SaveFile(fs, RichTextBoxStreamType.RichText);
                    break;
                case Constant.imageFile:
                    if (obj.imageBox.BackgroundImage != null && obj.imageBox.BackgroundImage != global::eFlash.Properties.Resources.defaultImage)
                    {
                        obj.imageBox.BackgroundImage.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                    break;
                case Constant.soundFile:
                    fs.Write(data, 0, data.Length);
                    obj.playerControl.setFile(Constant.ePath + eObj.actualFilename);
                    break;
                default:
                    throw new Exception("Invalid object type");
            }
            fs.Flush();
            fs.Close();
            fs.Dispose();

            return eObj;
        }