You can use the first command line parameter of your console application to recieve a path. Then, to use it, the user would drag and drop the png file into the exe (drag into the icon in explorer). The exe would then run taking as first argument the path of the file the user dragged, and then execute as normal.
In fact, if the user would drag and drop multiple files, they would each be taken as a separate command line argument.
When no file is dragged, you want to tell the user about how to use your exe. Plus, you can keep a Console.ReadLine
as alternative input method.
Addendum: You could drag and drop files into the console window, and it will behave as if the user typed the path. That should also work... in fact, if you put that Console.ReadLine
I mentioned above, both ways would work. Only one file, tested in the default console and powershell.
I let checking the file type up to you. In fact, it would also work with folders, handle them as you must (for example, you can use Directoy.GetFiles
or Directory.EnumerateFiles
).
The basic idea is something like this (without error checking):
static void Main(string[] args)
{
if (args.Length == 0 )
{
Console.WriteLine("Command line Usage: ConsoleAppName.exe filePath");
Console.WriteLine("You can drag and drop files into the executable to use it");
// Optional: take a path from Console.ReadLine
}
else
{
foreach (var filePath in args)
{
Console.WriteLine($"Processing: {filePath}");
// Optional: check if folder and handle correctly
ProcessFile(filePath);
}
Console.WriteLine("All done");
}
Console.WriteLine("[Press any key to exit]");
Console.ReadKey();
}
I am assuming the file check happens inside ProcessFile
You can, of course, reference System.Drawing
from your console application.
Instead of taking the extension or trying to get the header of the file. I would suggest to just try to load the image (Bitmap(string)
). That way, it will support all the formats that can be loaded by GDI+ (BMP, GIF, EXIF, JPG, PNG and TIFF), and will do it even if the extension is wrong. It also means you get an ArgumentException
if it isn't an image in one of the supported formats.