0

How to execute command like:

adb.exe kill-server

for the file located in the solution itself ?

Image

I have executed only after adding the adb to the PATH in environment variables:

private string implementCommandLine(string fileName, string param)
    {
        string output = "";

        ProcessStartInfo startInfo = new ProcessStartInfo(fileName, param)
        {
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        };

        Process adbProc;
        adbProc = Process.Start(startInfo);
        using (StreamReader myOutput = adbProc.StandardOutput)
        {
            output = myOutput.ReadToEnd();
        }

        return output;
    }

I tried as well to get the path of the adb.exe located in the Files folder

but it is really too confusing to get the path, i couldnt get it successfully !

Still when I execute "Files/adb.exe [params]" I get this error:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll

Exception Details:

Exception

MBH
  • 16,271
  • 19
  • 99
  • 149
  • Did you enable Copy Local in the properties of your exe file and assign filename as Files/abd.exe? – fofik Feb 05 '16 at 12:58
  • That doesn't help at all. What did you pass as filename? If it was just the executable's name, it would have to be at the same folder as your exe. Files do not get copied to the Debug or Release directory automatically, otherwise you'd get all code files in there. You have to set the item's Build Action property to Content, or add a post-build event to copy it to your Debug directory. Still, the file would be copied to `Files/abd.exe`, so your `fileName` should be `"Files/abd.exe" too. – Panagiotis Kanavos Feb 05 '16 at 12:58
  • What exeception are you getting, as you have successfully registered your exec to be executed from CMD globally. – Anil Feb 05 '16 at 13:02
  • @AnilKumar i added it to the q – MBH Feb 05 '16 at 13:04
  • I added the path of the adb myself to the PATH environment variable, then i could executed it and it worked, but this is not what will happen when i publish the app ! @AnilKumar – MBH Feb 05 '16 at 13:05
  • publish the app, where on different system? another request add more exception details if possible. – Anil Feb 05 '16 at 13:07
  • Add the .exe to your resources, and every time your application are to use it check if it exists. If not, write it to the disk from the resources. – Visual Vincent Feb 05 '16 at 13:08

2 Answers2

6

The folders you see in the Solution Explorer are there to organize your source code while you do development. In general, once you've built and deployed your program, those folders don't automatically have any meaning.

What you need to do is to ensure that the file you need to run is part of the build output, where you can know the location of the file relative to your executable ahead of time, and then make sure you deploy it that way. The easiest way to do this is to set the properties on the item of interest:

enter image description here

This will copy the item, intact, into the build output in the same folder structure as in your source code, so in your case your Debug folder would have:

Program.exe Program.pdb Files\adb.exe

Whenever you deploy your build, make sure you maintain that folder structure, and then you can find adb.exe relative to Program.exe using any of the usual ways to get the program's folder path,

Community
  • 1
  • 1
Michael Edenfield
  • 28,070
  • 4
  • 86
  • 117
  • It just worked ! now The path is Files/adb,exe and it WORKS like a charm! thank you – MBH Feb 05 '16 at 13:20
  • 1
    the path will only be `files\adb.exe` as long as nothing else changes the working directory of your application -- which is very easy to do. You should always use full path names to launch executables. – Michael Edenfield Feb 05 '16 at 13:21
  • u mean i need always to check if file exists ? – MBH Feb 05 '16 at 13:22
  • 1
    that's also usually a good idea, but I mean you shouldn't use `"files\adb.exe"` as your executable path; if something changes the working directory (say you pop up a file dialog), then that path will no longer be correct. You should use something like `Path.Combine(Assembly.GetExecutingAssembly().Location, "Files", "adb.exe")` to make sure you always use a full path name. – Michael Edenfield Feb 05 '16 at 13:24
2

First of all: make sure if you went to the options and selected "Copy if newer" or "copy always" for that file.

If that is right be sure to add System.Windows.Forms.Application.StartupPath to the path you want to execute that file.

And third thing is: Are you sure, it is your application (adb) that cannot be found and not some dependency (required dlls for exammple)? Validate this by putting a simple textfile.txt in that directory and try to start it. If that works, but adb does not, than the application is started correctly, but needs other libraries itself, that cannot be found.

Additionally: Show us, what is within your "filename" variable if you are still stuck.

Ole Albers
  • 8,715
  • 10
  • 73
  • 166