0

There is always a way to read CMD Output using C# console application like what mentioned and asking to this thread: How to parse command line output from c#?

However, what I need is the vice-versa of the former question, I want to create a Console application (whether in C# or in Vb - doesn't matter) that will return an an output that is readable by external program like cmd.exe.

I wish I could do as simple as:

class Program
{
    static DataTable Main(string[] args)
    {
        // do some stuff
        return myDataTable; //(or String)
    }
}

but with no luck.

Community
  • 1
  • 1
dr.Crow
  • 1,493
  • 14
  • 17
  • 1
    `Console.Write`/`Console.WriteLine` and other `Console` methods let you output to the console, but its very confusing what you want since cmd.exe is the command line so it doesn't "read" anything. – Ron Beyer Jun 03 '15 at 03:37
  • `main()` can't return complex objects like `DataTable` (it can return `void` or `int`, I believe). If you want an output of a complex object that is readable by another program, look at serialization. – Tim Jun 03 '15 at 03:44
  • Save as XML. Very simply. You need to add DataTable to Dataset. Then : DataSet ds = new DataSet(); DataTable dt = new DataTable(); ds.Tables.Add(dt); ds.WriteXml("filename", XmlWriteMode.WriteSchema); ds.ReadXml("filename"); – jdweng Jun 03 '15 at 05:29
  • specifically, i am using 4Dv12 as the final end point of the program. Since 4Dv12 has so many limitations I am using external scripts like vbs and cmd to extend my functionality. Now, i have this project that's to integrate in some programs, though I already have my alternatives and patches, i believe that the most efficient way is to let c# to do the whole integration thing let 4Dv12 catch it up. And if I can read C# console output using CMD, then i can do it as well in 4D directly. I forgot to mention that using XML is one of my last resort. – dr.Crow Jun 06 '15 at 07:31

2 Answers2

1

As the comments state reading the contents of a console window is very confusing. Why don't you redirect your console output to a file (and also the console if you wish) and then the solution becomes trivial.

This article describes how you can use your custom trace/console listener/logger essentially by extending a TextWriter and setting the Console.Out stream.

Community
  • 1
  • 1
Ruskin
  • 1,504
  • 13
  • 25
  • yes rediricting the output of csharp_console.exe console to a xml or json file was my last resort. Im hoping if I could do something li ke: `C:\\start sharp_console.exe 'someparamter'` return an output message in my cmd windows. ^_^ – dr.Crow Jun 06 '15 at 07:46
1

Have you considered using Windows PowerShell instead of Cmd.exe? PowerShell is able to operate on objects, either .NET or COM, rather than being limited to plain text as is the case with Cmd.exe. Therefore you don't need to worry about output at all - you just code your classes and then use them directly from PowerShell. So for example, you can do this:

PS C:\WINDOWS\system32> $list = New-Object System.Collections.ArrayList

PS C:\WINDOWS\system32> $list.Add("First item")
0

PS C:\WINDOWS\system32> $list.Add("Second item") 
1

PS C:\WINDOWS\system32> $list 
First item 
Second item

PS C:\WINDOWS\system32> $list.Count 
2

PS C:\WINDOWS\system32>

As you can see, it is easy to create objects and call their methods and properties from PowerShell. This is one of the reasons that PowerShell is now Microsoft's primary management interface. The ability to work with objects from a command line interface is really very powerful and PowerShell is well worth a look.

However, if that is not a possibility, then with a little more work you can get what you want using cmd.exe by simply writing to the console.

In a console application there are always three streams available, known as stdin, stdout and stderr ('standard input', 'standard output' and 'standard error'). stdout is normally connected to the display and stdin is normally connected to the keyboard input, so by default a console application gets input from the keyboard and writes output to the display. However, the streams can be redirected to/from a file, or directly to another program.

The > character redirects stdout to a file. Thus,

dir > myfiles.txt

writes output to myfile.txt instead of the display.

Similarly, output can be 'piped' directly to another program, as in:

dir | more

The pipe (|) character in the middle connects stdout from the dir command directly to stdin of the more command and thus data flows directly between the two programs. Note that dir is still just writing to the console, it doesn't necessarily know that its output has been redirected.

Using one of the above techniques, in combination with serialization, you could arrange to pass data between command line applications. You would have to arrange for your data to be serialized in a format that both programs can understand, and to make life easier using a format such as XML or JSON would be sensible, as it is easier to deserialize than plain text.

Tim Long
  • 13,508
  • 19
  • 79
  • 147
  • Note: it isn't completely clear what you are trying to achieve. I have answered what I _think_ you are trying to do but if I have misunderstood then please elaborate in a comment, or edit your question, and I will update my answer to match. – Tim Long Jun 03 '15 at 16:41
  • yes thanks, this was helpful too. but I was more focused on how to read the C# console output. Somehting like: `C:\\Start csharp.exe 'todo' ` and display the output message in my cmd window – dr.Crow Jun 06 '15 at 07:38