Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Using StreamReader error

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

    Using StreamReader error

    Hi - I have an indicator that I am working on that uses inputs from a text file. I was looking at the StreamReader example template in the NT help documentation and it uses the command:

    Code:
    sr = new System.IO.StreamReader(filename);​
    My understanding is that the following code is more effective in managing resources (the Dispose command is automatically executed):

    Code:
    using (StreamReader sr = new StreamReader(FilePath))
    However, when I use the "using" command, I receive an error on execution, the system can't find the file/path. It runs fine with the former command.

    Code:
    System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Users\xxxx\Documents\NinjaTrader 8\'.
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
      at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
      at System.IO.StreamReader..ctor(String path, Encoding encoding, Boolean detectEncodingFromByteOrderMarks, Int32 bufferSize, Boolean checkHost)
      at System.IO.StreamReader..ctor(String path)
      at NinjaTrader.NinjaScript.Indicators.CustomLevelsDis play.OnBarUpdate() in C:\Users\xx\Documents\NinjaTrader 8\bin\Custom\Indicators\Test.cs:lin e 145
    ​
    Why would I receive the error with the "using" command?

    Also, when using the initial "new System.IO.StreamReader" command, it locks up my file (I can't edit the file) while the indicator is running, since the Dispose method is in the State.Terminated section, should I relocate that code?

    Thanks

    #2
    Hello john_44573,

    From the code you provided the variable used for the file is different, is that potentially the issue? The first code uses filename and the second code uses FilePath. As long as the path is valid it shouldnt matter which form you use, ultimately both code will perform the same besides the using syntax encapsulates the code into a region where it is released after use.

    Comment


      #3
      Originally posted by NinjaTrader_Jesse View Post
      Hello john_44573,

      From the code you provided the variable used for the file is different, is that potentially the issue? The first code uses filename and the second code uses FilePath. As long as the path is valid it shouldnt matter which form you use, ultimately both code will perform the same besides the using syntax encapsulates the code into a region where it is released after use.
      Thank you for the reply. I don't think the variables were different at the time, but nonetheless, I tried the 'using' method again today and it worked.

      Comment

      Latest Posts

      Collapse

      Topics Statistics Last Post
      Started by Geovanny Suaza, 02-11-2026, 06:32 PM
      0 responses
      547 views
      0 likes
      Last Post Geovanny Suaza  
      Started by Geovanny Suaza, 02-11-2026, 05:51 PM
      0 responses
      323 views
      1 like
      Last Post Geovanny Suaza  
      Started by Mindset, 02-09-2026, 11:44 AM
      0 responses
      99 views
      0 likes
      Last Post Mindset
      by Mindset
       
      Started by Geovanny Suaza, 02-02-2026, 12:30 PM
      0 responses
      543 views
      1 like
      Last Post Geovanny Suaza  
      Started by RFrosty, 01-28-2026, 06:49 PM
      0 responses
      545 views
      1 like
      Last Post RFrosty
      by RFrosty
       
      Working...
      X