none
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the path 'D:\CsvLoggerResultTracker' is denied. RRS feed

  • Question

  • Hi,

    I wrote the below code to create a csv file and then to enter some sample test data in it. However it fails every time. I checked the permission for the folder as well and permission looks ok.

    My Code:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.IO;
    
    namespace CsvLogger
    {
        class Program
        {
            static void Main(string[] args)
            {
                Program pr = new Program();
                pr.createCSVLogger();
                pr.writeToCSVLogger("TC1_SampleTestCase", "Pass", DateTime.Now);
    
            }
    
            public void createCSVLogger()
            {
                string directoryPath = "D:\\CsvLoggerResultTracker";
                if (!Directory.Exists(directoryPath))
                {
                    Directory.CreateDirectory(directoryPath);
                }
    
                string filePath = directoryPath + "\\ExecutionResult.csv";
    
                if (!File.Exists(filePath))
                {
                    File.Create(filePath);
                }
    
                var objCSV = new StringBuilder();
                var firstColumn = "TestMethod";
                var secondColumn = "Result";
                var thirdColumn = "Date";
                var newLine = string.Format("{0}, {1}, {2}", firstColumn, secondColumn, thirdColumn);
                //var newLine = string.Format("{0}", firstColumn);
                objCSV.AppendLine(newLine);
    
                File.WriteAllText(filePath, objCSV.ToString());
            }
    
            public void writeToCSVLogger(string testcasename, string result, DateTime dt)
            {
                string directoryPath = @"D:\CsvLoggerResultTracker";
                string newLine = string.Format("{0}, {1}, {2}", testcasename, result, dt);
                File.AppendAllText(directoryPath, newLine);
            }
        }
    }
    

    Screen shot of error message I got:

    Security permission for the folder:

    Could anyone please let me know why the issue occurs ?

    Thank you,


    Ahetejazahmad Khan.

    Monday, October 16, 2017 7:51 AM

Answers

  • Hi,

    Sorry I sent a wrong path. Path of folder only, not the file.

    public

    voidwriteToCSVLogger(stringtestcasename, stringresult, DateTimedt)

            {

               

    stringdirectoryPath = @"D:\CsvLoggerResultTracker\ExecutionResult.csv";

               

    stringnewLine = string.Format("{0}, {1}, {2}", testcasename, result, dt);

               

    File.AppendAllText(directoryPath, newLine);

            }


    Ahetejazahmad Khan.

    • Marked as answer by Ahetejaz Monday, October 16, 2017 7:57 AM
    Monday, October 16, 2017 7:57 AM

All replies

  • Try This in your last method:

    string directoryPath = @"D:\CsvLoggerResultTracker\ExecutionResult.csv";

    instead of:

    string directoryPath = @"D:\CsvLoggerResultTracker\";

    You try writing the text to a folder, and not a file....



    • Edited by ThisNewbie Monday, October 16, 2017 8:19 AM
    Monday, October 16, 2017 7:57 AM
  • Hi,

    Sorry I sent a wrong path. Path of folder only, not the file.

    public

    voidwriteToCSVLogger(stringtestcasename, stringresult, DateTimedt)

            {

               

    stringdirectoryPath = @"D:\CsvLoggerResultTracker\ExecutionResult.csv";

               

    stringnewLine = string.Format("{0}, {1}, {2}", testcasename, result, dt);

               

    File.AppendAllText(directoryPath, newLine);

            }


    Ahetejazahmad Khan.

    • Marked as answer by Ahetejaz Monday, October 16, 2017 7:57 AM
    Monday, October 16, 2017 7:57 AM