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);
}
}
}
}
Access denied? Getting data from .txt to listbox
Page 1 of 14 Replies - 153 Views - Last Post: 06 March 2013 - 09:11 AM
#1
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.
Replies To: Access denied? Getting data from .txt to listbox
#2
Re: Access denied? Getting data from .txt to listbox
Posted 06 March 2013 - 07:08 AM
Your code:
is trying to read the current directory. It is not trying to read the file that you selected in the open file dialog.
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.
#3
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);
}
}
}
}
#4
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.
#5
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
http://msdn.microsof...g.openfile.aspx
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|