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 716671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:19:32+00:00 2026-05-14T05:19:32+00:00

Is there a managed API for kernel32.searchpath? i.e not using a pinvoke. http://www.pinvoke.net/default.aspx/kernel32.searchpath

  • 0

Is there a managed API for kernel32.searchpath?
i.e not using a pinvoke.

http://www.pinvoke.net/default.aspx/kernel32.searchpath

  • 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-14T05:19:33+00:00Added an answer on May 14, 2026 at 5:19 am
        static void Main(string[] args)
        {
    
            string lpPath = null;
            string lpFileName = "notepad";
            string lpExtension = ".exe";
            int nBufferLength = 255;
            string lpBuffer = "";
            string lpFilePart = "";
    
            int bufferSize = SearchPath(lpPath, lpFileName, lpExtension, nBufferLength, out lpBuffer, out lpFilePart);
    
        }
    
        private static int SearchPath(string lpPath, string lpFileName, string lpExtension, int nBufferLength, out string lpBuffer, out string lpFilePart)
        {
            // lpPath [in, optional] 
            // The path to be searched for the file. 
            // If this parameter is NULL, the function searches for a matching file using a registry-dependent system search path.
    
            //lpFileName [in] 
            //The name of the file for which to search.
    
            //lpExtension [in, optional] 
            //The extension to be added to the file name when searching for the file. The first character of the file name extension must be a period (.). The extension is added only if the specified file name does not end with an extension. 
    
            //If a file name extension is not required or if the file name contains an extension, this parameter can be NULL.
    
            //nBufferLength [in] 
            //The size of the buffer that receives the valid path and file name, in TCHARs.
    
            //lpBuffer [out] 
            //A pointer to the buffer to receive the path and file name of the file found. The string is a null-terminated string.
    
            //lpFilePart [out, optional] 
            //A pointer to the variable to receive the address (within lpBuffer) of the last component of the valid path and file name, which is the address of the character immediately following the final backslash (\) in the path.
    
            //Return Value
            //If the function succeeds, the value returned is the length, in TCHARs, of the string that is copied to the buffer, not including the terminating null character. If the return value is greater than nBufferLength, the value returned is the size of the buffer that is required to hold the path.
    
            //If the function fails, the return value is zero. 
    
            List<string> pathsToSearch = new List<string>();
            string currentWorkingFolder = Environment.CurrentDirectory;
            string path = System.Environment.GetEnvironmentVariable("path");
            lpBuffer = "";
            lpFilePart = "";
    
            if (lpPath == null)
            {
                RegistryKey key = Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Control\\Session Manager");
                object safeProcessSearchModeObject = key.GetValue("SafeProcessSearchMode");
                if (safeProcessSearchModeObject != null)
                {
                    int safeProcessSearchMode = (int)safeProcessSearchModeObject;
                    if (safeProcessSearchMode == 1)
                    {
                        // When the value of this registry key is set to "1", 
                        // SearchPath first searches the folders that are specified in the system path, 
                        // and then searches the current working folder. 
                        pathsToSearch.AddRange(Environment.GetEnvironmentVariable("PATH").Split(new char[] { Path.PathSeparator }, StringSplitOptions.None));
                        pathsToSearch.Add(currentWorkingFolder);
                    }
                    else 
                    {
                        // When the value of this registry entry is set to "0", 
                        // the computer first searches the current working folder, 
                        // and then searches the folders that are specified in the system path. 
                        // The system default value for this registry key is "0".
                        pathsToSearch.Add(currentWorkingFolder);
                        pathsToSearch.AddRange(Environment.GetEnvironmentVariable("PATH").Split(new char[] { Path.PathSeparator }, StringSplitOptions.None));
                    }
                }
                else
                {
                    // Default 0 case
                    pathsToSearch.Add(currentWorkingFolder);
                    pathsToSearch.AddRange(Environment.GetEnvironmentVariable("PATH").Split(new char[] { Path.PathSeparator }, StringSplitOptions.None));
                }
            }
            else
            {
                // Path was provided, use it
                pathsToSearch.Add(lpPath);
            }
    
            FileInfo foundFile = SearchPath(pathsToSearch, lpExtension, lpFileName);
            if (foundFile!= null)
            {
                lpBuffer = Path.Combine(foundFile.DirectoryName, foundFile.Name);
                lpFilePart = foundFile.Name;
    
            }
    
            return lpBuffer.Length;
        }
    
        private static FileInfo SearchPath(List<string> paths, string extension, string fileNamePart)
        {
            foreach (string path in paths)
            {
                DirectoryInfo dir = new DirectoryInfo(path);
                var fileInfo = dir.GetFiles().Where(file => file.Extension == extension && file.Name.Contains(fileNamePart));
                if (fileInfo.Any())
                    return fileInfo.First();
            }
            return null;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 441k
  • Answers 442k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: SHOW OPEN TABLES An check whether the column In_use… May 15, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer If I have understood correctly, you are referring to the… May 15, 2026 at 5:46 pm
  • Editorial Team
    Editorial Team added an answer You need a SelectedAdministrator property of type Administrator in your… May 15, 2026 at 5:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.