Open Program with dragging file (PDF) and recovering the path

1

Hello, I'm having a somewhat complicated need, I'm developing an application that sends pdf files to an application via webservices. I just want to let this problem start in some different ways. I'll try to explain by example. When I have a file in Work and drag it to a shortcut the same word is opened, and this happens with several programs. Or when you right click on a word file and in the context menu "send to" windows appears the word and it opens the file. I would like my application to work this way. I'm working C # Windows Forms in Visual Studio. I was able to get a similar function by dragging the file into the textBox with the DragDrop Event. I've done a lot of research on Google, but I do not know if I'm looking wrong, because I did not find anything. If someone else has done something similar or has some idea how to do it.

    
asked by anonymous 23.08.2017 / 21:36

1 answer

1

All you have to do is deal with the arguments you have started with the application. With this you can even call the application from the command line, like this:

enviarpdf.exe "c:\pdf\arquivo.pdf"

To treat the arguments, simply declare a string array as a parameter of the main method:

    static void Main(string[] args)
    {

        if (args.Length != 0)
        {
            string arg = args[0];
            MessageBox.Show(arg);
        }

     }

Because your application is winforms and considering you need the path inside Form1, it could be done like this:

    static void Main(string[] args)
    {

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(args.Lenght >0 ? args[0] : null));

     }

So, the first argument will be passed to Form1.

ps. Of course in Form1, you should put the constructor to get a parameter of type string.

public Form1(string _arquivo)
{
   InitializeComponent();
   ArquivoPdf = _arquivo;
}
    
23.08.2017 / 21:46