-1

jaja. folks, my first post was too extensive, let me try again. i think it is important to always be polite when asking for help and to fully communicate one's motivations, so one liners would not be the best vehicle to do this but i will adapt to how things are done in this forum. chances are it will be helpful to split my question into modules.

  • i have this piece of code in easylanguage that generates one unique text file with a unique filename every time one of four different types of events takes place (every individual text file serves as its own separate log entry). i need to convert this code to c# (i will use this code inside the ninjatrader trading platform), using streamwriter ot any other c# solutions that would allow me to generate unique text files with filenames created dynamically that would include some string inputs, variables and date and time with a similar format to the following:

//    pass in a string containing line to be written to the file         
method void recordevent(string msg)     
variables:     
string filepath;     
begin     
    //    create file name, including a directory based upon the symbol     
    //     
    //    note:   symbol should not contain characters that are not valid in file names     
    //            indicator name should not contain invalid characters for a file name     
    //            add whatever other parameters desired         
    filepath = string.format("{0}\{1}_{2}_{3:yyyyMMddhhmmss}_{4}_{5}.txt",     
        iprimarydirectory,     
        filnamplusymtic,
        gronam,     
        datetime.now,
        casnum,
        numtostr(price,2));

    print(filepath);
    sw = streamwriter.create(filepath);  
    sw.autoflush = true;    
    sw.writeline(msg);      
    sw.close();     
end;

[note: primary directory, file name plus symbol ticker and group name are string inputs to be defined by the user while case number is a string variable to be calculated by the program. i use this information to make sure filenames are unique and to keep files sorted by symbol ticker and time they were generated.]

very well, i hope this version of my initial question will be more understandable and manageable. thanks to all, regards.

brthmrkmn
  • 1
  • 2
  • Does this answer your question? [Create a .txt file if doesn't exist, and if it does append a new line](https://stackoverflow.com/questions/9907682/create-a-txt-file-if-doesnt-exist-and-if-it-does-append-a-new-line) – NicholaiRen Oct 29 '19 at 19:25
  • 1
    is there a particular reason you need to use streamwriter and you can't use something more Csharpian like `System.IO.File.WriteAllLines(path, List)` or `System.IO.File.WriteAllText(path, string)` ? – hexagod Oct 29 '19 at 19:38
  • hi welcome to SO - the first 11 lines of your post could be removed - and I'm fairly sure if you thought about it you could reduce the above post in size considerably. try to be concise it makes it easier for us to see what you are trying to do. – Tim Rutter Oct 29 '19 at 19:41
  • Could the above post be boiled down to "How in c sharp does one append a line of text to file?" – Tim Rutter Oct 29 '19 at 19:43
  • thanks. if there are better methods to create these files in c# i will look into them, i wanted to use streamwriter because i received a recommendation to use it and i have managed to make it work in the platform i was working with previously. however, i will now read on the commands that have been recommended. – brthmrkmn Oct 29 '19 at 20:17

1 Answers1

0

I'm not going to try and decipher your entire post. If the question is just how do you append a line to a file (creating it if it doesnt exist), then its pretty simple in c sharp:

private void WriteFile(string text)
    {
      // lock a generic object to ensure only one thread is accessing the following code block at a time
      lock (lockObj)
      {
          string filePath = Path.Combine(@"C:\ntlogs\",$"{DateTime.Now.ToString("yyyyMMdd HHmmss")}.txt");
          File.AppendAllText(filePath, text);
      }

If there are other specific problems you face then create a separate question and ask about that specifically without all the additional information

Tim Rutter
  • 4,549
  • 3
  • 23
  • 47
  • I was a little thrown off by the fact they were asking about dynamically creating filenames, which made me assume they were writing files not lines, but yes this looks good. Gotta love the `System.IO.File` namespace =] – hexagod Oct 29 '19 at 19:54
  • it seems odd that its creating a new file every second. I'd question the validity of that. – Tim Rutter Oct 29 '19 at 19:55
  • Also if its logging you seek I can highly recommend the log4net assembly available through nuget. It take all the hard work out of writing to log files. There're tutorials on how to use it – Tim Rutter Oct 29 '19 at 19:55
  • thanks. i will look into all your suggestions but i want to generate one unique file with a unique filename per event, appending a file multiple times would not be useful. and there are only 5 to 10 events a week, i use time down to seconds to make sure that filenames will be unique. – brthmrkmn Oct 29 '19 at 20:11
  • They are only unique once a second so you need to be sure your events can't happen twice in a second - however if it does that the appendalllines will just add to the same file twice. as I say logging with log4net will do all this. It will create a log file entry with a timestamp if you want that. – Tim Rutter Oct 29 '19 at 20:13