Error : while creating a file.

Posted by Harikrishna on Stack Overflow See other posts from Stack Overflow or by Harikrishna
Published on 2010-05-05T06:22:26Z Indexed on 2010/05/05 6:28 UTC
Read the original article Hit count: 327

Filed under:
|
|
|
|

I am storing records of datatable to csv file with the help of the following code :

// we'll use these to check for rows with nulls
var columns = yourTable.Columns
    .Cast<DataColumn>();

using (var writer = new StreamWriter(yourPath)) {
    for (int i = 0; i < yourTable.Rows.Count; i++) {
        DataRow row = yourTable.Rows[i];

        // check for any null cells
        if (columns.Any(column => row.IsNull(column)))
            continue;

        string[] textCells = row.ItemArray
            .Select(cell => cell.ToString()) // may need to pick a text qualifier here
            .ToArray();

        // check for non-null but EMPTY cells
        if (textCells.Any(text => string.IsNullOrEmpty(text)))
            continue;

        writer.WriteLine(string.Join(",", textCells));
    }
}

But I get the error like

System.UnauthorizedAccessException was unhandled
  Message="Access to the path 'D:\\Harikrishna\\Monthly Reports' is denied."

What can be the problem and solution ?

© Stack Overflow or respective owner

Related posts about c#

Related posts about winforms