Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6771355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:25:19+00:00 2026-05-26T15:25:19+00:00

What I want is something like the which command: provide a filename (exe, bat…)

  • 0

What I want is something like the “which” command: provide a filename (exe, bat…) and return the full path of that file:

which java.exe
c:\windows\system32\java.exe

The code would be like:

string fileName = "java.exe"
string fullPath = PathResolver.Resolve(fileName);

Do we have such facility in .NET framework?

Thanks.

Update:
Finally, I wrote one myself:

  // Reoslve the full path of a file name
    // fileName: of absolute path or relative path; with ext or without ext
    static string ResolvePath(string fileName)
    {
        // 0. absolute path
        string[] stdExts = { ".bat", ".cmd", ".pl", ".exe" };
        if (Path.IsPathRooted(fileName))
        {
            if (File.Exists(fileName))
            {
                return fileName;
            }
            else
            {
                foreach (string eachExt in stdExts)
                {
                    string fullPath = fileName + eachExt;
                    if (File.Exists(fullPath))
                    {
                        return fullPath;
                    }
                }
            }

            return "";
        }

        // 1. candidate extensions
        string fileNameNoExt = Path.GetFileNameWithoutExtension(fileName);
        string ext = Path.GetExtension(fileName);
        string[] candidateExts;
        if (string.IsNullOrEmpty(ext))
        {
            candidateExts = stdExts;
        }
        else
        {
            string[] exts = { ext };
            candidateExts = exts;
        }

        // 2. candidate path:
        //      http://msdn.microsoft.com/en-us/library/windows/desktop/ms682586(v=vs.85).aspx#search_order_for_desktop_applications
        List<string> candidatePaths = new List<string>();

        // application dir
        string fileApp = Process.GetCurrentProcess().MainModule.FileName;
        candidatePaths.Add(Path.GetDirectoryName(fileApp));

        // current dir
        candidatePaths.Add(Directory.GetCurrentDirectory());

        // system dir
        candidatePaths.Add(Environment.SystemDirectory);

        // windows dir
        string winDir = Environment.GetEnvironmentVariable("windir");
        candidatePaths.Add(winDir);

        // PATH
        string[] paths = Environment.GetEnvironmentVariable("PATH").Split(';');
        foreach (string path in paths)
        {
            // strip the trailing '\'
            candidatePaths.Add(Path.GetDirectoryName(path));
        }

        // 3. resolve
        foreach (string eachPath in candidatePaths)
        {
            foreach (string eachExt in candidateExts)
            {
                string fullPath = eachPath + "\\" + fileNameNoExt + eachExt;
                if (File.Exists(fullPath))
                    return fullPath;
            }
        }

        return "";
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T15:25:20+00:00Added an answer on May 26, 2026 at 3:25 pm

    I don’t know if this can help you:

        public string PathResolver(string filename)
        {
            string[] paths = Environment.GetEnvironmentVariable("PATH").Split(';');
            foreach (string path in paths)
            {
                string fname = Path.Combine(path, filename);
                if (File.Exists(filename)) return fname;
            }
            return "";
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

:bn , :bp just switches buffers I want something like ctrl-o, which has a
I want to do something like String.Format([{0}, {1}, {2}], 1, 2, 3) which returns:
My sql table is something like (message,created) I want to select those rows which
I have some MET data I want to validate which would look something like
I want something like this: <msxsl:script language=C#> ??? getNodes() { ... return ... }
Is there a command like cat in linux which can return a specified quantity
Say that I have simple command line application, that looks something like this #
I want something like an std::map , but I only want to see if
i want something like this the user enter a website link i need check
I want something like a canvas, but where i'd be able to manipulate pixels

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.