I want to read modules from .exe - .dll files!

Tell me how to achieve the effect of Drag and Drop in the console?

I want to do this: if you moved the wrong format to display errors that they say the wrong format) and continue.

If you moved the file you need, we work with it ...

This option does not work:

 try { if (Path.GetExtension(args[0]) == ".exe" || Path.GetExtension(args[0]) == ".dll") { Console.WriteLine("Working.../"); } else Console.WriteLine("Format is not correct"); } catch { } Console.ReadKey(); 

    2 answers 2

    In newer versions of windows, dragging a file into the console causes the full path to the file to be typed in the console. You can Console.ReadLine() usual Console.ReadLine() . Do not forget to remove quotes (for example, the .Trim('"') method .Trim('"') ).

    Or you can take a file as a parameter (the same args[0] ). But in this case, the user will have to drag the file not to the console window, but to your program.

    In the latter case, the user can also click on the file with the right button, select the "open with" option and select your program there. Most likely, it will be an easier way than Dran'n'Drop

    • When "open with" - the path to the file will be transferred to the program by the first argument - user241285
    • @monobogdan first argument has index 0. This is a feature of arrays in C-like languages, if you did not know ... - Pavel Mayorov
    • @monobogdan but seriously - run and check. In .NET, the name of the program itself is not transferred to the Main function, unlike C / C ++. - Pavel Mayorov
    • one
      argument 0 is the path to the executable itself, the first parameter is already the "file name" - user241285
    • one
      @monobogdan read my comments again. - Pavel Mayorov

    You can do this by hooking the console window.

    To do this, you need to get the console windows HWND:

     [DllImport("kernel32.dll")] public static IntPtr GetConsoleWindow(); IntPtr cmdHandle = GetConsoleWindow(); 

    Then you need to register Drag and Drop using RegisterDragDrop

    More information about RegisterDragDrop:

    https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms678405(v=vs.85).aspx

    • Yes, I am sure that it is possible, but I don’t know how, selecting a couple of applications that could do it) .. - GooliveR
    • Well, I just say, you can hook the console window, but most likely not - user241285
    • 3
      You get the HWND console windows (GetConsoleWindow (), this is the winapi function), then drag n drop through winapi in the old fashioned way. See msdn msdn.microsoft.com/en-us/library/windows/desktop/… - user241285
    • four
      And here it is argued that RegisterDragDrop will not work. Have you tried your code? - VladD
    • one
      @monobogdan: I see. But still, have you tried? - VladD