I am following Pcap.net tutorials from its wiki on github. I tried to run code from here:
https://github.com/PcapDotNet/Pcap.Net/wiki/Pcap.Net-Tutorial-Handling-offline-dump-files
I didn't understand the following part:
if (args.Length != 1)
{
Console.WriteLine("usage: " + Environment.GetCommandLineArgs()[0] + " <filename>");
return;
}
But I run this code and Bingo, nothings happen (no output).
I tried to figure out and found that args has the value "0".
I comment return command and it start working fine till I got IndexOutOfBound exception here:
using (PacketDumpFile dumpFile = communicator.OpenDump(args[0]))
Did I missed any thing?
This program require 1 argument (i.e. 'filename'in this case) that you should pass through command line.
Compile the code and run the program through command line with argument value.
For Example:
In CLI >MyProg.exe fileNmae
Related
I am having some trouble trying to work my way through some C# that I'm just not comfortable with. My issue appears to stem from a dash/hyphen in one of my parameters being passed to Process.Start(). In the below code the argument list is built from data in the database. The issue occurs when sRecipient has a dash. For example Bank-Name vs BankName. Unfortunately, I can't remove the dash because it is the name of a public key provided by the bank.
sGPGParms = " -e ";
if (sUseGPGASCIIArmor.ToUpper() == "Y")
{
sGPGParms += "-a ";
}
sGPGParms += "-r \"" + sRecipient + "\" \"" + sDestinationDir + sFileName + "\"";
The next section of code is where things seem to fail. The program is using Process.Start() to pass the path and the parameters. The program does not fall into the fail condition and says that the file created successfully but it doesn't do anything. This code worked earlier today before the bank provided a new encryption key with a dash in the name. That dash is literally the only change. This code has worked since 2008 so I have isolated it to the arguments parameter in Process.Start() and how the dash is impacting things as the culprit.
Process myCmd;
if ((myCmd = Process.Start(Path, sGPGParms)) == null)
{
LogEvent(sServerLogPath, "ERROR: GPG.exe failed to start", sUserName);
SqlContext.Pipe.Send("GPG.exe failed to start");
}
else
{
LogEvent(sServerLogPath, "Bank file created successfully", sUserName);
SqlContext.Pipe.Send("Bann file created successfully");
}
As a side note, I can run the full command copied from the logging and it works. It only fails when passed through Process.Start().
This is the command that works manually but fails when passed through Process.Start() with no public key
"C:\Program Files (x86)\GNU\GnuPG\gpg2.exe" -e -r "Bank-Name" "c:\public\BankPay.txt"
Any ideas why this dash is causing an issue?
UPDATE: Working through some of the suggestions in the comments I see that I am getting an exit code of 2 with the error public key not found. The key exists because I can run the command manually and can see it when I --list-keys
I develop an application with command line parameters and use it in cmd shell and powershell. There it is obvious that the arguments are received differently in main() of my application.
static void Main(string[] args)
{
// In cmd shell: args[0] == "-ipaddress=127.0.0.1"
// In powershell: args[0] == "-ipaddress=127"
// and args[1] == ".0.0.1"
}
Example:
myApp.exe -ipaddress=127.0.0.1
In my C# application the arguments are interpreted differently depending on the shell I start the app in.
In cmd shell: arg[0]=-ipaddress=127.0.0.1
In powershell: arg[0]=-ipaddress=127 and arg[1]=.0.0.1
What is best practice here?
Should I join all args[] and parse the arguments in my application?
Should I rely on the shell's parser?
I would abandon cmd shell support completely and just created proper PowerShell cmdlet. Writing a Windows PowerShell Cmdlet. But I don't know your exact requirements.
In general calling executable from cmd and PowerShell should work same way. For example line, "> ping -n 3 google.com" just works fine no matter where you put it.
tl;dr:
When calling from PowerShell, enclose the entire argument in '...' (single quotes) to ensure that it is passed as-is:
myApp.exe '-ipaddress=127.0.0.1'
You've run into a parsing bug[1]
where PowerShell breaks an unquoted argument that starts with - in two at the first .
The following simplified example demonstrates that:
# Helper function that echoes all arguments.
function Out-Argument { $i = 0; $Args | % { 'arg[{0}]: {1}' -f ($i++), $_ }}
# Pass an unquoted argument that starts with "-" and has an embedded "."
Out-Argument -foo=bar.baz
The above yields:
arg[0]: -foo=bar
arg[1]: .baz
[1] The presumptive bug is present as of Windows PowerShell v5.1 / PowerShell Core v6.0.1 and has been reported on GitHub.
A PSv2 variation of the bug affects . when enclosed in "..." in the interior of the argument - see this answer of mine.
I'm trying to execute the Windows find command from LINQPad but it isn't working and I don't understand why. This is my LINQPad script and I'm executing it in LINQPad as C# Statement(s).
string find = #"find ""Processing request to "" ""Y:\Services\DynaMiX.Services.DatabaseMaintenance\*.log""";
find.Dump("find");
var results = Util.Cmd(find);
results.Dump();
The find.Dump("find") statement displays the following as expected.
find "Processing request to " "Y:\Services\SteveC.Services.DatabaseMaintenance\*.log"
When I copy that and paste it in a CMD window it executes the find as it should but when running the script in LINQPad it throws CommandExecutionException with the ErrorText FIND: Parameter format not correct.
Can anyone shed any light on why this might happen?
Turns out to be an incorrect use of Util.Cmd on my part. Instead of Util.Cmd(string commandText) I needed to use the Util.Cmd(string commandText, string args) overload. The following statement works.
var results = Util.Cmd("find", #"""Processing request to "" ""Y:\Services\SteveC.Services.DatabaseMaintenance\*.log""");
I'm creating a small commandline tool for a customer and trying to verify its output. The output is been written to the commandline itself. Since its rather a huge file that's been used as an input file and thus a huge out to write to the commandline, I wanted the output to redirect to a file.
Usually I use use commandline arguments like so to redirect the output to a file:
a.exe ./input.txt > ./ouput.txt
However, in my program, I try to verify the input:
static void Main(string[] args)
{
if (args.Length != 1)
throw new ArgumentException();
...
And args now is:
args[0] = ./input.txt
args[1] = >
args[2] = ./ouput.txt
Honestly I personally still expect only one argument, since the file is been created and thus the shell does understand what I mean. So... what am I doing wrong? Should I use args or something else?
Thank you in advance!
Are you passing the arguments through Visual Studio? It will only work if you untick Enable the Visual Studio hosting process.
Image and explanation from here.
I am getting an error when running the above code and don't know the exact issue. What is the solution for this?
using System;
class second
{
static void Main(string[] args)
{
Console.WriteLine("Hello, {}!", args[0]);
Console.WriteLine("Welcome to the C# station tutorial!");
Console.ReadLine();
}
}
The error is "Index out of range".
You are missing the argument position
Console.WriteLine("Hello,{0}!", args[0]);
The string[] args parameter is the command-line parameters to the exe. I'm guessing that you didn't pass any arguments, therefore args[0] is out of range, just like it said. If you ran your exe as:
your.exe MyName
then it would have at least got past the IndexOutOfRangeException - to raise a FormatException instead ;p To fix that, change the {} to {0}.
This is what worked for me. Check to make sure that there are parameters. If not, provide feedback on the console.
if (args.Length == 0)
{
Console.WriteLine("No input parameters were received.");
return;
}
I believe you ran the program off Visual Studio and thus had run the program without passing in any parameter against arg[].
To pass in a parameter E.g. Scott to arg[],
go to the Solution Explorer (menu View → Solution Explorer)
select Properties.
Input "Scott" into the "Command Line arguments" textbox under the Start Options section.
The program will run as normal. See the image below.
If args has zero items, then attempting to get the first index of it (i.e. 0) will cause an index out of bounds exception. You should check that args isn't null or empty before attempting to reference an index within it. If you ran that example with some command line arguments it would probably work.