Forum guidelines
C# can not find executable
Posted By dc on 10 Oct 2012
In a C# 2008 application, I am having the program execute another C# 2008 application. I am getting the error message, "The system cannot find the file specified".

Thus the second program is not located.

To solve this problem, where should the executable be located for the first program to find it?

In the deployment package, do I need to place the executable in the same directory path as where the main program is located at?

Re: C# can not find executable
Posted By Vulpes on 10 Oct 2012  
I'd definitely place the other executable in the same directory as the main application or (if it needs its own support files) in a sub-directory of that directory.

You'll have least hassle that way :)

  • 0
  • 0
Re: C# can not find executable
Posted By dc on 10 Oct 2012  
Thank you for your answer!
I have the following additional questions:

1. Why is it less hassle to put the executable of the second application in the same directory as the first program? Does the .net framework assume some of default directory if no directory is specified? If Microsoft assumes a default directory can  you tell me where the default directory is locatecd at?

2. When my company deployed my C#  2008 and 2010 applications to a test server, one of the applications did not use the configuation set for where the executable was suppose to be at? Our network administrator found where the first program thinks the second executable was at.
Thus I am trying to determine why one of the programs did not take the setting it was given. 
The location the first program was looking at was at:
c:/server name/share directory name/test/app1.exe. 

The only way I can think this location was picked since on a few tries,  the location of the executable as was at:

//server name/share directory name/test/app1.exe

Is this accidently? Should I have the network administrator reboot the test server to see if the c:/server name/share directory name/test/app1.exe. location request goes away?

3. To fix this problem permanently, should I have the executable value of the second location in a static  variable name?  What do you suggest?

Re: C# can not find executable
Posted By Vulpes on 11 Oct 2012  
As a general rule, .NET framework methods assume that a file will be in the current working directory if you don't specify a full path. This is usually the same as the executable's directory but, if for some reason it isn't, then there are a number of easy ways to get the latter such as Application.StartupPath for a windows forms application.

The following way should work for any type of application:

string path = System.IOPath.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

I don't know what's causing the problem you mention but, if you always store other executables which the main executable calls in the latter's directory (or a sub-directory thereof), then you can obtain that directory using the above method and store it in a global variable when the main application starts. 

Provided that the application has been installed correctly, you should then be able to find any file your application needs using an absolute path for the client machine, rather than relying on the vagaries of relative paths. 

  • 0
  • 0
Re: C# can not find executable
Posted By dc on 11 Oct 2012  
I have a few addtional questions:
1. The statement you listed above, how would I change it to find a specific location of the executable?
2. Should I just change the location of the second executable to be just the name of the executable alone without a directory path assigned to it? That way by default, the program will know where the second program is located at?
3. Should I change the path of the second executable to be a static variable so that it does not change?
4. Should the second executable be setup as a reference  in the visual studio 2010 ide where the first progam is executed from? Can you tell me  the reason why or why not for adding the second progtram as a reference to the first program?

  • 0
  • 0
Re: C# can not find executable
Posted By Vulpes on 11 Oct 2012   Accepted Answer
Taking those questions in the same order:

1. The statement doesn't need changing. It finds the directory of the current application i.e. the current executing assembly in .NET parlance.

2. I'd use a full path for the second executable which (if it's in the same directory as the  main executable) can always be constructed using the directory in 1. above:

string secondPath = path + "\\" + "second.exe"; // or whatever

3. I'd construct the path to the second executable when the main executable starts and store it in a global static field so it can be accessed later from any part of the application. For example, if you use the Program class:

static class Program
   private static string _secondPath;  

   internal static string SecondPath 
      get { return _secondPath; }

   static void Main(string[] args)
      string path = System.IOPath.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
      _secondPath = path + "\\" + "second.exe";

     // rest of code


You can then access it from elsewhere in the application with Program.SecondPath.

4. References are for class libraries (i.e. dlls) that your application uses - not for stand alone executables that your application launches.



Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.