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.
    JesseNinjaTrader Customer Service

    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 lucyhendricks, Today, 04:18 PM
      0 responses
      3 views
      0 likes
      Last Post lucyhendricks  
      Started by cmarkb, 05-08-2020, 09:42 AM
      11 responses
      736 views
      0 likes
      Last Post rafaelcoisa  
      Started by reynoldsn, Today, 03:08 PM
      1 response
      3 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by RubenCazorla, 09-15-2022, 08:28 AM
      4 responses
      56 views
      0 likes
      Last Post NinjaTrader_Jesse  
      Started by termitikicloud, 01-11-2025, 05:27 PM
      12 responses
      72 views
      0 likes
      Last Post termitikicloud  
      Working...
      X