none
unhandled exception of type 'System.IO.IOException' occurred in system.xml.dll The process cannot access the file "filename" because it is being used by another process.

    Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO;
    using System.Security.Permissions;
    using System.Security.AccessControl;
    namespace Iphone_Picture_Viewer
    {
        public partial class IPV : Form
        {
            public string path;
            private string pathcrypt;
            private string prefix="encrypggt";
            private const string code="encrypt";
            public Bitmap bit;
            public IPV()
            {
                InitializeComponent();
            }

            private void IPV_Load(object sender, EventArgs e)
            {

            }

            private void button3_Click(object sender, EventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {

            }

            private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
            {

            }

            private void button4_Click(object sender, EventArgs e)
            {
                try
                {
                    OpenFileDialog open = new OpenFileDialog();
                    open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";
                    if (open.ShowDialog() == DialogResult.OK)
                    {
                         bit = new Bitmap(open.FileName);
                         path = open.FileName;
                         imagePath.Text = path;

                    }  

                }
                catch (Exception)
                {
                    throw new ApplicationException("Failed loading image");
                }
            }

            private void button3_Click_1(object sender, EventArgs e)
            {

            }

            private void button2_Click_1(object sender, EventArgs e)
            {
                pictureBox1.Image = pictureBox1.BackgroundImage; 

            }



            private void button1_Click(object sender, EventArgs e)
            {

               pathcrypt = imagePath.Text;
               EncryptFile();
               DecryptFile();


            }



            private void EncryptFile()
        {                            
            byte[] ImageBytes;
            if ((pathcrypt != path) & (code == prefix))
            {
                ImageBytes = File.ReadAllBytes(path);

                for (int i = 0; i < ImageBytes.Length; i++)
                {
                    ImageBytes[i] = (byte)(ImageBytes[i] + 5);
                }
                FileSecurity fSec = File.GetAccessControl(path);
                fSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
                File.SetAccessControl(path, fSec);

                File.WriteAllBytes(path, ImageBytes);
            }  


        }

        private void DecryptFile()
        {

            byte[] ImageBytes;

            if ((pathcrypt == path) & (code != prefix))

            {
                ImageBytes = File.ReadAllBytes(path);

                for (int i = 0; i < ImageBytes.Length; i++)
                {
                    ImageBytes[i] = (byte)(ImageBytes[i] - 5);
                }

                FileSecurity fSec = File.GetAccessControl(path);
                fSec.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
                File.SetAccessControl(path, fSec);


                File.WriteAllBytes(path, ImageBytes);

                pictureBox1.Image = bit;
            }  

        }
        }
    }

    this is my code pleas help i need it for a class project tomorrow plz

                                                                                                                                                                                                                                      
    Thursday, June 06, 2013 11:02 PM

All replies

  • I'm taking a WILD guess, but here:

                         bit = new Bitmap(open.FileName);
                         path = open.FileName;
                         imagePath.Text = path;

    You never close the file, so you try to open it twice consecutively. So it might be having trouble opening a file that's already open.

    Friday, June 07, 2013 12:54 AM