0

I have read a lot of answers on this issue, but none of them helps for me. Now, it's been 5 years that I had C# and apperantly I've forgotten it all. But I like to get into the language again to use it for automation. So, here is the bit of code I already have:

{
    string path = @"C:\Users\decraiec\Documents\Client Automated";
   //In this folder I will find all my XML files that I just want to load in a textbox

    public Form1()
    {
        InitializeComponent();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        //create a way to read and write the files
        //go get the files from my harddrive
        StreamReader FileReader = new StreamReader(path);
        //make something readable for what you have fetched
        StreamWriter FileWriter = new StreamWriter(textBox1.ToString());
        int c = 0;
        while (c == FileReader.Read())
        {
            string load = FileReader.ReadToEnd();//read every xmlfile up to the end
            string stream = FileWriter.ToString();//make something readable
        }

        try
        {
            textBox1.Text = FileWriter.ToString();//what you have made readable, show it in the textbox
            FileWriter.Close();
        }
        finally
        {
            if (FileReader != null)
            { FileReader.Close(); }
        }
        if (FileWriter != null)
        { FileWriter.Close(); }
    }
}

If I run this code like this I'll get:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\Users\decraiec\Documents\Atrias Automated' is denied.

While I was hoping to see all the XML files in the textbox listed and clickable ( - although I need to insert the clickable code yet ) I've been looking in my folder and subfolder and files and I do have admin rights on everything. About the [ mscorlib.dll ] I have no clue where to find this.

Now if I wrap the StreamReader in a use ( var....;) VS doesn't recognizes it (red lines under the words) saying that I'm missing an instance of an object or something else of issue (just trying to glue the things together).

Could someone try to get me in the right direction please?

SiHa
  • 7,830
  • 13
  • 34
  • 43
Chantal D
  • 3
  • 2
  • change files c colon into some other directory like D – Balaji Marimuthu Jan 26 '17 at 12:28
  • A different problem in your code: `textBox1.ToString()` will return `System.Windows.Forms.TextBox, Text: `, not the contents of the textbox, use `textBox1.Text` for that. – Lasse V. Karlsen Jan 26 '17 at 12:30
  • Are you running the program under the `decraiec` user? – Lasse V. Karlsen Jan 26 '17 at 12:30
  • Does the **file** `C:\Users\decraiec\Documents\Client Automated` exist? Meaning that there is a **file** named `Client Automated` under the users `Documents` folder? – Lasse V. Karlsen Jan 26 '17 at 12:31
  • In path you're not specify the file name – Balaji Marimuthu Jan 26 '17 at 12:31
  • Judging by the amount of odd code in your example here I would say you should cut it all out and paste it into notepad, then put one line at a time back into your program and determine what it should do and then if that line is the best/correct way to do it. There's odd loops, local variables that aren't used, calling `.ToString()` on odd objects that will return the names of the types involved, not their content, etc. – Lasse V. Karlsen Jan 26 '17 at 12:34

2 Answers2

0

I think your path is a directory, not a file. Almost the exact same issue was addressed here: Question: Using Windows 7, Unauthorized Access Exception when running my application

What you can do is create a DirectoryInfo object on the path and then call GetFiles on it. For example:

DirectoryInfo di = new DirectoryInfo(directoryPath);

Foreach(var file in di.GetFiles())
{
    string pathToUseWithStreamReader = file.FullName;
}
Community
  • 1
  • 1
0

You need to use Directory.GetFiles to get any files residing in your "Client Automated" folder, then loop through them and load every single file into the stream.

var files = Directory.GetFiles(path);
foreach (var file in files)
{
    var content = File.ReadAllText(file);
}

You can read more on it here:
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx

Also - in general, when working with files or directories like this, it's a good idea to programmatically check if they exist before working with them. You can do it like so:

if (Directory.Exists(path))  
{  
    ...
}  

Or with files:

if (File.Exists(path))  
{  
    ...
}
lmms90
  • 243
  • 2
  • 6