Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

c# - Backing up and reloading console command history

When using a C# console app, all the commands I input get buffered during the life of the session (ie. until I close the Console).
I would now like to be able to back up this buffer and retrieve it next time I relaunch my console app.
I have read a few threads on SOF and got some pointer to "SetConsoleHistoryInfo", but I am not sure that is the way to go, nor how to use it for that matter.
Has anyone ever done something similar?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could read each command you input and save it into a .txt file. If you are writing directly to the console you can save the string from the Console.ReadLine() function into a variable and then write it to the historical file.

The next time you run your program, have a method that checks the .txt file and retrieves the commands in whatever format you desire (array, line by line etc.)

This gives you flexibility as to how you manage the .txt file as well, you can keep the historical commands from every time your program has run or just the previous execution.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...