Problem with FileStream in C # (Access denied to the path)

0
Buen día tengan ustedes.

I have the following problem, I use Visual Studio 2013, I have created a system where the basic operations are done (search, delete, enter), linked to a database created in SQL SERVER 2008.

The problem is when I tried to enter a new record with a photo, it tells me the following:

Acceso denegado a la ruta de acceso 'C:\Users\Mi-Pc\Documents\Visual Studio 2013\EDICION CON BUNIFU\SISTEMA\SISTEMA \bin'.

The code that I use where I get the part of the error is the following:

private void buniVista_Click(object sender, EventArgs e)
    {
        if (txtnumero.Text == "")
        {
            MessageBox.Show("Digite Numero del modelo para Continuar","SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            txtnumero.Focus();
        }
        else if (txtdescripcion.Text == "")
        {
            MessageBox.Show("Digite la descripcion para Continuar", " SISTEMA", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            txtdescripcion.Focus();
        }
        else if (dtpfecha.Text == "")
        {
            MessageBox.Show("Digite fecha para Continuar", " SISTEMA ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            dtpfecha.Focus();
        }
        else if (txtTalla.Text == "SELECCIONE TALLA")
        {
            MessageBox.Show("Seleccione talla para Continuar", " SISTEMA ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            txtTalla.Focus();

        }
        else if (txtexaminar.Text == "")
        {
            MessageBox.Show("Cargue una fotografia para Continuar", " SISTEMA ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            btnexaminar.Focus();

        }
        else
        {
            FileStream stream = new FileStream(txtexaminar.Text, FileMode.Open, FileAccess.Read);
            //Se inicailiza un flujo de archivo con la imagen seleccionada desde el disco.
            BinaryReader br = new BinaryReader(stream);
            FileInfo fi = new FileInfo(txtexaminar.Text);

            //Se inicializa un arreglo de Bytes del tamaño de la imagen
            byte[] binData = new byte[stream.Length];
            //Se almacena en el arreglo de bytes la informacion que se obtiene del flujo de archivos(foto)
            //Lee el bloque de bytes del flujo y escribe los datos en un búfer dado.
            stream.Read(binData, 0, Convert.ToInt32(stream.Length));

            ////Se muetra la imagen obtenida desde el flujo de datos
            picfotografia.Image = Image.FromStream(stream);

            VistaModelo f2 = new VistaModelo();
            this.Hide();
            f2.Show();

            f2.lblnumero.Text = txtnumero.Text.ToString();
            f2.lbldescripcion.Text = txtdescripcion.Text.ToString();
            f2.lblfecha.Text = dtpfecha.Text.ToString();
            f2.lbltalla.Text = txtTalla.Text.ToString();
            f2.txtexaminar2.Text = txtexaminar.Text.ToString();
            f2.picfoto.Image = Image.FromStream(stream);
        }
    }

The error is specifically in this part:

FileStream stream = new FileStream(txtexaminar.Text, FileMode.Open,FileAccess.Read);
    
asked by Ryuzaki Lpz 23.02.2017 в 18:50
source

2 answers

1

That error is sent to you because you want to make FileStream to a folder, which should be a file. Try adding the name of the file, as an example I'll put it in a static way:

FileStream stream = new FileStream(txtexaminar.Text + "NombreArchivo.txt", FileMode.Open, FileAccess.Read);
    
answered by 23.02.2017 в 20:00
0

reading your code I think the problem is basically here

FileStream stream = new FileStream(txtexaminar.Text, FileMode.Open, FileAccess.Read);

You must make sure that the content of txtexaminar.Text is a valid path to a file in whose containing folder you have sufficient permissions. If a path is not indicated in txtexaminar.Text , the folder where it is located is used by default  the assembly that executes the code, in this case

  

C: \ Users \ My-Pc \ Documents \ Visual Studio 2013 \ EDITION WITH   BUNIFU \ SYSTEM \ SYSTEM \ bin

So I recommend you do the following

string imagePathFolder = System.Configuration.ConfigurationManager.AppSettings["ImagePathFolder"]; // recuperar de configuración la carpeta de las imágenes
FileStream stream = new FileStream(System.IO.Path.Combine(imagePathFolder, txtexaminar.Text), FileMode.Open, FileAccess.Read); // acceder al archivo de la carpeta de las imágenes
    
answered by 14.06.2017 в 08:23