-1

So I'm making a program to type what's in my clipboard into a text file, I plan on later transporting this information into an AHK script, but I want to do this all in one program, so if possible it will append to the .ahk file, but instead of it appending to the very last line, I need it to append to the line before return, which is the final line of the file.

Send ::redeem N4E2vzCEp {enter}
Sleep 1000
return

That's the end of the file, if possible I want my program to do something like:

string pasted = Clipboard.GetText();
sw.WriteLine.SecondLastLine("Send ::redeem " + pasted + " {enter}");
sw.WriteLine.SecondLastLine("Sleep 1000"); //Fully aware that secondlastline is not a valid command

But I don't know what the proper way of actually coding this would be.

Current code:

    private void paste_Click(object sender, EventArgs e)
    {
        string path = @"c:\users\john\desktop\auths.txt";
        using (StreamWriter sw = File.AppendText(path))
        {
            string pasted = Clipboard.GetText();
            sw.WriteLine("Send ::redeem " + pasted + " {enter}");
            sw.WriteLine("Sleep 1000");
        }

    }
Matheus Lacerda
  • 5,983
  • 11
  • 29
  • 45
John Olivas
  • 309
  • 1
  • 2
  • 14
  • 3
    SO is not a code writing service; Please update your question with the code you've tried so far. – Soviut Aug 14 '17 at 05:22
  • In what frequency do you want to append a line? – Ben Aug 14 '17 at 05:23
  • I'm not sure I understand Ben, what do you mean by frequency? Every button press will append the clipboard to the text file – John Olivas Aug 14 '17 at 05:25
  • What I wanted to know was how often you append lines to the file. "By button pressed" answered this ;) – Ben Aug 14 '17 at 05:28
  • possible duplicate https://stackoverflow.com/questions/8243122/how-to-write-data-at-a-particular-position-in-c and https://stackoverflow.com/questions/16212127/add-a-new-line-at-a-specific-position-in-a-text-file – MJ Khan Aug 14 '17 at 05:31

1 Answers1

1

What you can do is reading all lines into a List and then insert the new line at a specific position and write the lines back to the file.

List<string> lines = File.ReadAllLines("your file").ToList();
lines.Insert(lines.Count - 2, "new line");
File.WriteAllLines("your file", lines);
Ben
  • 763
  • 1
  • 5
  • 14
  • When I use this, it gives this: An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll Additional information: The process cannot access the file 'c:\users\john\desktop\auths.txt' because it is being used by another process. – John Olivas Aug 14 '17 at 05:45
  • At what line do you get the exception? – Ben Aug 14 '17 at 05:46
  • on 25, which is this: List lines = File.ReadAllLines(path).ToList(); and 'path' is this: string path = @"c:\users\john\desktop\auths.txt"; – John Olivas Aug 14 '17 at 05:47
  • Like the exception told you, there is another process which is accessing the file. Maybe the file is opened by another program or you have used it in your code before. – Ben Aug 14 '17 at 05:54
  • Only thing that has it open is notepad++, which I closed it out of that, and still gives the error. With the way I originally had it, I could have notepad++ open while using the program, and could press in notepad when I want to update it to the new changes. – John Olivas Aug 14 '17 at 06:03
  • Just saw thew streamwriter thing maybe it's whats doing it cause its technically pulling the path as well, so I commented it out to see if it fixes it – John Olivas Aug 14 '17 at 06:05
  • Make a new file (i.e. auths2.txt), a new path variable path2 and only use this variable in the code above. Just to make sure, you arn't using the file somewhere else in your code. – Ben Aug 14 '17 at 06:07
  • Okay, after some fiddling around with my clipboard, and commenting out sections of the code itself, it's working now. Thank you very much – John Olivas Aug 14 '17 at 06:09