是否有kernel32.searchpath托管的API? [英] Is there a managed API for kernel32.searchpath?

查看:91
本文介绍了是否有kernel32.searchpath托管的API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有kernel32.searchpath托管的API? 即不使用的PInvoke。

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

解决方案

 静态无效的主要(字串[] args)
    {

        字符串lpPath = NULL;
        字串lpFileName的对象=记事本;
        字符串lpExtension =.EXE;
        INT nBufferLength = 255;
        字符串lpBuffer =;
        字符串lpFilePart =;

        INT BUFFERSIZE = SearchPath中(lpPath,lpFileName的对象,lpExtension,nBufferLength,出lpBuffer,出lpFilePart);

    }

    私有静态诠释的SearchPath(字符串lpPath,串lpFileName的对象,串lpExtension,诠释nBufferLength,出字符串lpBuffer,出字符串lpFilePart)
    {
        // lpPath [中,可选]
        //要搜索的文件的路径。
        //如果这个参数为NULL,则函数搜索使用注册表相关的系统搜索路径匹配的文件。

        // lpFileName的对象[输入]
        //要搜索的文件的名称。

        // lpExtension [中,可选]
        //扩展要搜索的文件时添加到该文件名。的文件扩展名的第一个字符必须是一个句点(。)。扩展仅添加如果指定的文件名不带扩展名结尾。

        //如果不需要的文件扩展名或文件名包含扩展名,这个参数可以为NULL。

        // nBufferLength [输入]
        //接收有效的路径和文件名,在返回tchars缓冲区的大小。

        // lpBuffer [出]
        //一个指向接收缓冲区中的文件的路径和文件名。字符串是一个空结尾的字符串。

        // lpFilePart [出,可选]
        //的指针变量以接收有效的路径和文件名的最后一个组件,这是紧接在最终反斜杠(\)在路径中以下的字符的地址的地址(内lpBuffer)。

        //返回值
        //如果函数成功,返回的值是复制到缓冲区,不包括终止空字符的字符串的长度,在返回tchars。如果返回值大于nBufferLength,返回的值是需要保持路径的缓冲区的大小。

        //如果函数失败,返回值是零。

        名单<字符串> pathsToSearch =新的名单,其中,串>();
        字符串currentWorkingFolder = Environment.CurrentDirectory;
        字符串路径= System.Environment.GetEnvironmentVariable(路径);
        lpBuffer =;
        lpFilePart =;

        如果(lpPath == NULL)
        {
            的RegistryKey键= Registry.LocalMachine.OpenSubKey(\\ SYSTEM \\ CURRENTCONTROLSET控制\\会话管理器);
            对象safeProcessSearchModeObject = key.GetValue(SafeProcessSearchMode);
            如果(safeProcessSearchModeObject!= NULL)
            {
                INT safeProcessSearchMode =(INT)safeProcessSearchModeObject;
                如果(safeProcessSearchMode == 1)
                {
                    //当该注册表项的值设定为1,
                    // SearchPath中首先搜索系统路径中指定的文件夹中,
                    //然后搜索当前工作文件夹。
                    pathsToSearch.AddRange(Environment.GetEnvironmentVariable(PATH)斯普利特(新的char [] {} Path.PathSeparator,StringSplitOptions.None));
                    pathsToSearch.Add(currentWorkingFolder);
                }
                其他
                {
                    //当该注册表条目的值设置为0,
                    //计算机首先搜索当前工作文件夹,
                    //并然后搜索系统路径中指定的文件夹中。
                    //此注册表项,系统默认值为0。
                    pathsToSearch.Add(currentWorkingFolder);
                    pathsToSearch.AddRange(Environment.GetEnvironmentVariable(PATH)斯普利特(新的char [] {} Path.PathSeparator,StringSplitOptions.None));
                }
            }
            其他
            {
                //默认值0的情况下
                pathsToSearch.Add(currentWorkingFolder);
                pathsToSearch.AddRange(Environment.GetEnvironmentVariable(PATH)斯普利特(新的char [] {} Path.PathSeparator,StringSplitOptions.None));
            }
        }
        其他
        {
            //路径被提供,使用它
            pathsToSearch.Add(lpPath);
        }

        FileInfo的foundFile = SearchPath中(pathsToSearch,lpExtension,lpFileName的对象);
        如果(foundFile!= NULL)
        {
            lpBuffer = Path.Combine(foundFile.DirectoryName,foundFile.Name);
            lpFilePart = foundFile.Name;

        }

        返回lpBuffer.Length;
    }

    私有静态的FileInfo SearchPath中(表<字符串>路径,字符串扩展,串fileNamePart)
    {
        的foreach(在路径字符串路径)
        {
            DirectoryInfo的DIR =新的DirectoryInfo(路径);
            VAR的fileInfo = dir.GetFiles()式。(文件=> file.Extension ==扩展和放大器;&安培; file.Name.Contains(fileNamePart));
            如果(fileInfo.Any())
                返回fileInfo.First();
        }
        返回null;
    }
 

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

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

解决方案

    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;
    }

这篇关于是否有kernel32.searchpath托管的API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆