.NET Equivalente del comando "where" del símbolo del sistema

In .NET, is there an equivalent of the "where" command that is present on Windows Vista and Windows 7 machines? I need to resolve the hard path to an executable that is defined in the system path at install time, and not everyone installs everything using the default installation path.

Currently I call the "where" command as an external process, but this only works on Vista and Windows 7 machines, this does not work on XP out of the box.

preguntado el 08 de noviembre de 11 a las 19:11

Short answer: no. Are you calling this other executable from something like Process.Start? Or are you looking to put files/something in its folder? -

I am calling the executable using Process.Start(). -

You could write a function using System.Environment.GetEnvironmentVariable to get the path, and then search the path yourself for the executable you are looking for. -

@BasedAsFunk: I'm certain that Process.Start takes the PATH environment variable into consideration. However, it does have an odd caveat where it does not like quoted paths. -

What process are you starting such that you need where? -

2 Respuestas

You have to read the environment variable PATH and look for the presence of the application in each of the path and use that. That is how the Powershell cmdlet get-command funciona también - http://technet.microsoft.com/en-us/library/dd347726.aspx

Note: If the reason for getting the path through which / where is to use the path and call the executable, you need not do that since the executable is already in PATH and you need get the path and then use it explicitly.

respondido 08 nov., 11:23

Well, here's the funny thing: when I use 'where', it finds it, but if I merely call Process.Start() using only the executable name, it doesn't find it. This is a behavior that only seems to be present on 64 bit Vista and Windows 7, 32-bit machines have been working fine. - Bender el más grande

Did this 32bit code erroneously believe it was installed to `C:\Program Files`? - user7116

@BasedAsFunk - I don't observe something like that. Works fine on 64 bit Windows 7. - manojlds

@sixlettervariables - I suppose that is possible, but I'm not sure how a mix up like that would occur. Also, uninstalling and reinstalling this particular program fixed the issue without a code change (presumably because it re-added it's bin directory to the front of the PATH), is there a limitation if your PATH variable is too long? - Bender el más grande

You can use the System.IO.Directory.EnumerateFiles() method to search for files Here's an example

   System.IO.Directory.EnumerateFiles(Environment.SystemDirectory,"YourFile.exe",SearchOption.AllDirectories)

It returns an array of string containing the path(s).

respondido 30 nov., 11:21

"System.IO.Directory.Exists" takes a single parameter as input which is the path of the directory and returns true/false. From where did you get this definition of Exists method? - Pawan Mishra

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.