C
C#2y ago
uselessxp

✅ ConsoleApp - run the .exe by cmd and catch given parameters

Well, as a little task I'm trying to code a ConsoleApp that should base64 encrypt an input string and automatically copy it to the clipboard. The ideal result is the following, by cmd: - -base64.exe -e string (for encrypt) - -base64.exe -d string (for decrypt) First of all I played with encoding/decoding and I figured out that I have to convert the input string into a byte (array?), then after I can apply the method that convert to base64. I succesfully "wrote" the encrypt method, now something I don't know how to do, it to catch one or more parameter, I don't know how is technically called this, I'd have googled it. But what I finally understood, is the meaning of:
static void Main(string[] args)
static void Main(string[] args)
I always wondered which is the sense to write string[] args if the program works perfectly without arguments, and now I know that the meaning of this is somehow to accept and handle cmd.exe parameters as arguments.
27 Replies
uselessxp
uselessxp2y ago
I made a try and with surprise, I got this error: Operator '==' cannot be applied to operands of type 'string[]' and 'string'
if (args == "e") Console.WriteLine("encoding . . .");
if (args == "e") Console.WriteLine("encoding . . .");
I don't understand why but when I normally make a compare with strings using == I don't get any errors. Ok, it works using this form
if (args[0] == "-e") Console.WriteLine("encoding . . .");
if (args[1] == "-d") Console.WriteLine("decoding . . .");
if (args[0] == "-e") Console.WriteLine("encoding . . .");
if (args[1] == "-d") Console.WriteLine("decoding . . .");
Becquerel
Becquerel2y ago
string[] means an array of string - a collection. you got that error because it doesn't make sense to compare a collection of things to a single thing Main() gives you an array of strings because people can give as many arguments as they want. to parse them you have to pluck out the individual arguments you want from the array, as you've done
uselessxp
uselessxp2y ago
Yeah I solved specifying the position of the array to check. In my case i checked position 0 for -e and position 1 for -d, but I have a doubt. In all consoleapps you can type arguments in whatever order you want, did they use some existing function, like "if array contains" ?
Anton
Anton2y ago
the Array class has a bunch of static helpers
uselessxp
uselessxp2y ago
using System.Text;

namespace base64
{
internal class Program
{
static void Main(string[] args)
{
if (args[0] == "-e") Console.WriteLine($"Encoded string is: {base64encode(args[1])}");
if (args[0] == "-d") Console.WriteLine($"Decoded string is: {base64decode(args[1])}");
//Console.WriteLine($"Encoded string is: {Convert.ToBase64String(Encoding.UTF8.GetBytes(Console.ReadLine()))}");
}

static string base64encode(string input)
{
byte[] plainTextBytes;
string output;

plainTextBytes = Encoding.UTF8.GetBytes(input);
output = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));

return output;
}

static string base64decode(string base64EncodedData)
{
byte[] base64EncodedBytes;
string output;

base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
output = Encoding.UTF8.GetString(base64EncodedBytes);

return output;
}
}
}
using System.Text;

namespace base64
{
internal class Program
{
static void Main(string[] args)
{
if (args[0] == "-e") Console.WriteLine($"Encoded string is: {base64encode(args[1])}");
if (args[0] == "-d") Console.WriteLine($"Decoded string is: {base64decode(args[1])}");
//Console.WriteLine($"Encoded string is: {Convert.ToBase64String(Encoding.UTF8.GetBytes(Console.ReadLine()))}");
}

static string base64encode(string input)
{
byte[] plainTextBytes;
string output;

plainTextBytes = Encoding.UTF8.GetBytes(input);
output = Convert.ToBase64String(Encoding.UTF8.GetBytes(input));

return output;
}

static string base64decode(string base64EncodedData)
{
byte[] base64EncodedBytes;
string output;

base64EncodedBytes = Convert.FromBase64String(base64EncodedData);
output = Encoding.UTF8.GetString(base64EncodedBytes);

return output;
}
}
}
Something I don't understand, is why the compiler force me to set base64encode() and base64decode() as static instead of public, when I studied modifiers, I read that public is the most open modifier that make an object accessible from everyone from everywhere, and static make the same thing, but only if you remain in the same class, so public should works anyway with this logic.
ero
ero2y ago
things can be public static
Anton
Anton2y ago
public has nothing to do with static
uselessxp
uselessxp2y ago
that's ok, but the point is that the program require static as mandatory
ero
ero2y ago
because the main method is static
uselessxp
uselessxp2y ago
oh so all change now damn, most of guides about modifiers just explain the effect they have on variables
Anton
Anton2y ago
you should either learn some basics of classes, or just ignore it for now and use static everywhere also don't declare your variables up front it's not C99
uselessxp
uselessxp2y ago
I always used public everywhere till now, but I was told that's ok but not a good way to write the code
Anton
Anton2y ago
public is completely independent of static it's a completely different feature
uselessxp
uselessxp2y ago
so I should declare and assign at the same time
ero
ero2y ago
or not declare at all
string base64encode(string input)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
}
string base64encode(string input)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(input));
}
you never use plainTextBytes in that method btw
uselessxp
uselessxp2y ago
I thought about this solution, idk if is always a good practice to reduce the code as max I can
ero
ero2y ago
absolutely not if you feel like making the declarations, that's fine
string base64encode(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
string b64 = Convert.ToBase64String(bytes);

return b64;
}
string base64encode(string input)
{
byte[] bytes = Encoding.UTF8.GetBytes(input);
string b64 = Convert.ToBase64String(bytes);

return b64;
}
uselessxp
uselessxp2y ago
first to understand of args[] works I just put all in the main() for try base64encoding, and I was able to reduce all in one row, it was cool but maybe less clear, I made this but I reverted it
Console.WriteLine($"Encoded string is: {Convert.ToBase64String(Encoding.UTF8.GetBytes(Console.ReadLine()))}");
Console.WriteLine($"Encoded string is: {Convert.ToBase64String(Encoding.UTF8.GetBytes(Console.ReadLine()))}");
I also putted Console.ReadLine() in the same row I learned this reduction method by writing Excel formula
ero
ero2y ago
yeah i'd say try to avoid that it's a nuance and there's no real rule for it you just have to do what feels right
uselessxp
uselessxp2y ago
ok I added this little check to the Main() since I noticed that the app immediately crashes if I try to debug from VS since find the array empty (since the console call the Main() method without args). My doubt is, if for some reason I don't handle every possible errors, and the app crashes, is there a way to generate a .txt or a .log that contains the error description?
static void Main(string[] args)
{
if (args.Length != 0)
{
if (args[0] == "-e") Console.WriteLine($"Encoded string is: {base64encode(args[1])}");
if (args[0] == "-d") Console.WriteLine($"Decoded string is: {base64decode(args[1])}");
}
}
static void Main(string[] args)
{
if (args.Length != 0)
{
if (args[0] == "-e") Console.WriteLine($"Encoded string is: {base64encode(args[1])}");
if (args[0] == "-d") Console.WriteLine($"Decoded string is: {base64decode(args[1])}");
}
}
I remember a dev of a program, in the first releases putted try catch + clipboard.settext(exception) literally everywhere, telling users to forward him errors, but I don't know if is a good practice to spam this statement everywhere.
ero
ero2y ago
again, setting the clipboard of the user is bad practice already there are libraries which handle option parameters like this for you
uselessxp
uselessxp2y ago
yeah I know this, I mean that this dev catched all errors with try catch, maybe instead of set the text to clipboard I could generate a .log
Anton
Anton2y ago
I believe you've learned about guard clauses. make one that displays how to call the program correctly if there are no args you can just print it to the console and pipe it into a file program.exe -e > file
ero
ero2y ago
i would just rely on others to do the work i don't feel like doing like Spectre.Console can't mess up user input with that
uselessxp
uselessxp2y ago
very cool^^ now I understand much things I seen on some beauty consoleapps I seen
uselessxp
uselessxp2y ago
btw this is an example, but what about if my app crashes for another error that I have not considered? normally, if I'm not wrong, the user playing the app will just see "application.exe stopped working" without any explanations, so he'll never able to tell me what really happened. a solution (maybe dirty) would be to put try catch on every method and every actions the app make, in the paste I did it creating some random codes, example code error 1, code error 2, and so, in that way when the user tell me the code error I can identify the bugged piece of code easily.
uselessxp
uselessxp2y ago
https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.trace?redirectedfrom=MSDN&view=net-7.0 Idk if this could works for what I'm looking for. I read also about logging libraries like Serilog, but I think it's not for errors reporting.
Trace Class (System.Diagnostics)
Provides a set of methods and properties that help you trace the execution of your code. This class cannot be inherited.
Want results from more Discord servers?
Add your server
More Posts
❔ Bad IL Format Exception when porting .NET Framework 4.8 Project to .NET Core 6.0Hi, I've currently got the glorious task to port a .NET Framework 4.8 project to .NET Core 6.0. So f❔ "Cannot assign requested address [::1]:27017" when connecting to MongoDB on Docker.I have a Docker Compose file with containers - `backend`: ASP.NET Core web API - `mongo`: MongoDB wh❔ External Login Callback (Facebook works, Google doesn't)This is copied from a youtube tutorial and I've been trying to step through and figure out the probl❔ Need someone who is an expert at rating resumes, would really appreciate itMy resume https://cdn.discordapp.com/attachments/684475244664193242/1065827594806763580/Yehor-Belenk❔ Handling different version changes for external standard.Hey! I'm developing tools for a community to support OpenDRIVE standard. The name of the standard do❔ IMAP vs POP3 for getting attachments from an emailI'm building a console app in C# using MailKit to find and save all of the attachments in my outlook❔ WPF MVVM ModelViews and ViewsI have recently learned that ModelView can't have UI elements in it (bad practice). Hence, couple of❔ RatelimitHello, i am currently trying to program a discord bot. Ive created a method to track how many player❔ WPF export binding into resourcesHi! How can I define a binding with the ability to use intellisense ```xml <UserControl.Resource❔ properties/constructor question```cs class Car { public string model; public string color; public int year; // Create a cl