2

I have simple command line application, and I want to store commands typed in between starts of program.

Storing alone is not problem, I know how to do it, but how I can restore it? Console class don't have any method for setting history, if I press up arrow on start of application it is empty.

Methods from msdn for unmanaged code are not helpful for me, good answer could show me how to use them in c# to get what I need.

My idea is to override up arrow with ReadKey only and do it "hard" way but if there is easier way I would be glad.

Mateusz
  • 2,287
  • 20
  • 28
  • Isn't the built-in functionality enough? http://stackoverflow.com/questions/6973260/why-does-my-console-application-have-command-history – jeroenh Nov 21 '12 at 10:15
  • Not, as you see I want to store it between program runs, built-in functionality is nice but when I close app, and open it again I don't have history. See bash in linux it has commands even if you close it and open again, not so much windows console. – Mateusz Nov 21 '12 at 10:46
  • History is a feature of the command processor, cmd.exe. The equivalent of bash. It doesn't lose history because you don't close it between program runs. You'll have to build your own if you want this to be available in your own program. Certainly not impossible with Console.ReadKey() – Hans Passant Nov 21 '12 at 11:31
  • Yes and I just want to take advantage of that feature which I have when my command line application starts, so I want to restore saved session to use it between console windows starts, and that is what I want to know how to do. – Mateusz Nov 21 '12 at 13:07

3 Answers3

1

I would simply save the commands as XML or in a relational database, when needed I will deserialize the XML to the appropriate objects stored as a list or possibly an array (if you have a defined number of commands, for instance the last 10). Then override the appropriate event as you mentioned and iterate the list of command objects by keeping a counter of where you are in the list.

Andre Lombaard
  • 6,985
  • 13
  • 55
  • 96
  • I would not use database, or XML for such task, plain text with new lines is enough. I would make it simply cycling through array of strings, no need for objects. I found that overriding Console.ReadLine() default behavior is rather impossible so that is the main problem. – Mateusz Nov 22 '12 at 07:32
1

I know this is an old question, but I was searching for an answer too. Couldn't find one though, so I built InteractivePrompt. It's available as a NuGet Package and you can easily extend the code which is on GitHub. It features a history for the current session, but I plan to implement functionality to save the commands between sessions.

It's a very useful package for wrapping DLLs, like SQLite for instance.

sorrell
  • 1,801
  • 1
  • 16
  • 27
0

In case someone looks for something like it, I ended up using powershell and set of scripts, but I managed to setup powershell history:

https://software.intel.com/en-us/blogs/2014/06/17/giving-powershell-a-persistent-history-of-commands

$HistoryFilePath = Join-Path ([Environment]::GetFolderPath('UserProfile')) .ps_history
Register-EngineEvent PowerShell.Exiting -Action { Get-History | Export-Clixml $HistoryFilePath } | out-null
if (Test-path $HistoryFilePath) { Import-Clixml $HistoryFilePath | Add-History }
# if you don't already have this configured...
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward

Save that code to file: C:\Users\\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Mateusz
  • 2,287
  • 20
  • 28