Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Exporting List to .csv Fails: Access to the path is denied.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Exporting List to .csv Fails: Access to the path is denied.

    Hi there,
    I am using a strategy to log stats and generate a report. At the end of a trading period (3 months) I need to export/write to a .csv file. Before I get too far down the debug rabbit hole, I'd like to ask if anyone has any insight on the error shown below in the event it's an issue with file permissions inside of Windows. Here's the error:
    Strategy 'ArgyleOpt': Error on calling 'OnBarUpdate' method on bar 25260: Access to the path 'C:\Program Files (x86)\NinjaTrader 8\bin64\CanTradeWeekends.csv' is denied.

    As I go through the path C:\Program Files (x86)\NinjaTrader 8\bin64, RMB on each folder > properties, they all show as Read-only.
    I think this is an issue. IIf I change this, then hit Okay, it looks like it takes, but if I RMB > properties again they always go back to Read-only. I even went back to the root folder and tried this. It sat there for about 5 minutes "changing" everything, but again, when I RMB > properties, they're all back to Read-Only. I think this could the source of the problem, I just don't know how to fix it.

    Any assistance from the community would be greatly appreciated. Thank you for your time and help in advance!

    Nathan.

    Other potentially relevant information:
    • The code does fine without my ExportData function, and prints to Ninja's Output window without error. However, I need to do this on a much larger scale, so I want to send it to a .csv for further analysis.
    • ExportData code...
      Code:
      public static class ExportData
      	{
      	// https://gist.github.com/luisdeol/c2c276796a92c8e3246ce2cd3e17e1df
      	public static void ExportCsv<T>(List<T> genericList, string fileName)
      	{
      	var sb = new StringBuilder();
      	var basePath = AppDomain.CurrentDomain.BaseDirectory;
      	var finalPath = Path.Combine(basePath, fileName + ".csv");
      	var header = "";
      	var info = typeof(T).GetProperties();
      	if (!File.Exists(finalPath))
      	{
      	// If the file does no exist, then create it, and add a header row.
      	var file = File.Create(finalPath);
      	file.Close();
      	foreach (var prop in typeof(T).GetProperties())
      	{
      	header += prop.Name + ", ";
      	}
      	header = header.Substring(0, header.Length - 2);
      	sb.AppendLine(header);
      	TextWriter sw = new StreamWriter(finalPath, true);
      	sw.Write(sb.ToString());
      	sw.Close();
      	}
      	foreach (var obj in genericList)
      	{
      	// Adds each value of info to a line, and puts a "," to serparate each. Rinse and repeat
      	sb = new StringBuilder();
      	var line = "";
      	foreach (var prop in info)
      	{
      	line += prop.GetValue(obj, null) + ", ";
      	}
      	line = line.Substring(0, line.Length - 2);
      	sb.AppendLine(line);
      	TextWriter sw = new StreamWriter(finalPath, true);
      	sw.Write(sb.ToString());
      	sw.Close();
      	}
      	}
      	}

    #2
    Hello NateG0310,

    Thank you for your post.

    I wouldn't recommend writing files to the Program Files NinjaTrader folder, I would recommend writing them to the User directory in your Documents as you should have the correct permissions to write to that.

    For example:

    path = NinjaTrader.Core.Globals.UserDataDir + "MyTestFile.csv"

    You can take a look at our example in our help guide that demonstrates using a StreamWriter in our help guide here that illustrates:



    Please let us know if we may be of further assistance to you.

    Comment


      #3
      Awesome! Problem solved. Thanks!!

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by NullPointStrategies, Yesterday, 05:17 AM
      0 responses
      65 views
      0 likes
      Last Post NullPointStrategies  
      Started by argusthome, 03-08-2026, 10:06 AM
      0 responses
      139 views
      0 likes
      Last Post argusthome  
      Started by NabilKhattabi, 03-06-2026, 11:18 AM
      0 responses
      75 views
      0 likes
      Last Post NabilKhattabi  
      Started by Deep42, 03-06-2026, 12:28 AM
      0 responses
      45 views
      0 likes
      Last Post Deep42
      by Deep42
       
      Started by TheRealMorford, 03-05-2026, 06:15 PM
      0 responses
      50 views
      0 likes
      Last Post TheRealMorford  
      Working...
      X