4 Replies - 468 Views - Last Post: 06 March 2013 - 09:11 AM Rate Topic: -----

#1 Afrosephurai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-February 13

Access denied? Getting data from .txt to listbox

Posted 06 March 2013 - 02:46 AM

Why am I getting this error, I tried going into the folder setting and unchecking read only box yet it keeps coming back, I also tried running as admin.

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;

namespace Random_Number_File_Reader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
                
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstboxInput.Items.Clear(); 
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            try
            {

                openFileDialog1.InitialDirectory = "@C:\\Users\\Joseph\\Desktop";
                openFileDialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*"; // File extensions "______(.__)
                openFileDialog1.FilterIndex = 1;

                openFileDialog1.ShowDialog();

                //Used .NET STREAMREADER and initialized and declared variable "inputfile"
                StreamReader inputfile;
                inputfile = File.OpenText(".");

                lstboxInput.Items.Add(inputfile.ReadLine());
            }

            catch (Exception xyz)
            {
                MessageBox.Show(xyz.Message);
            }


            
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Access denied? Getting data from .txt to listbox

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

Re: Access denied? Getting data from .txt to listbox

Posted 06 March 2013 - 07:08 AM

Your code:
50                inputfile = File.OpenText(".");


is trying to read the current directory. It is not trying to read the file that you selected in the open file dialog.
Was This Post Helpful? 1
  • +
  • -

#3 Afrosephurai  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 17-February 13

Re: Access denied? Getting data from .txt to listbox

Posted 06 March 2013 - 08:37 AM

Now I am getting a error saying the given path format is not supported.

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;

namespace Random_Number_File_Reader
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit(); 
                
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lstboxInput.Items.Clear(); 
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void btnGet_Click(object sender, EventArgs e)
        {
            string url1 = "@C:\\Users\\Joseph\\Documents\\Visual Studio 2012\\Projects\\Random_Number_File_Reader\\Random_Number_File_Reader\\bin\\Debug";

            try
            {

                openFileDialog1.InitialDirectory = ".";
                openFileDialog1.Filter = "txt files(*.txt)|*.txt|All files(*.*)|*.*"; // File extensions "______(.__)
                openFileDialog1.FilterIndex = 1;

                openFileDialog1.ShowDialog();

                //Used .NET STREAMREADER and initialized and declared variable "inputfile"
                StreamReader inputfile;
                inputfile = File.OpenText(url1);
                

                lstboxInput.Items.Add(inputfile.ReadLine());
            }

            catch (Exception xyz)
            {
                MessageBox.Show(xyz.Message);
            }


            
        }
    }
}



Was This Post Helpful? 0
  • +
  • -

#4 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Access denied? Getting data from .txt to listbox

Posted 06 March 2013 - 08:51 AM

@ sign goes outside the "" marks and you only need one \ if you are using it. Double \\ if you aren't.
Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,467
  • Joined: 05-May 12

Re: Access denied? Getting data from .txt to listbox

Posted 06 March 2013 - 09:11 AM

You do know that you can call OpenFile() on the OpenFileDialog class, right?

http://msdn.microsof...g.openfile.aspx
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1