如何将执行路径与文件路径和参数分开

How to separate a executing path to file path and parameter?

本文关键字:路径 参数 文件 执行      更新时间:2023-10-16

有这样的行

C:Program FilesRealtekAudioHDARAVCpl64.exe -s

在注册表项中

HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionRun

现在,我想将绝对路径和参数从直线中分离出来。

如果线路是

C:Space DirDot.DirSample Name.Dot.exe param path."C:Space DirDot.DirSample Name.Dot.exe"

我应该用哪个分隔符来处理这行?是否有任何Windows API函数可以解决此问题?

您想要的函数在Windows中可以使用的标准C库中。

char theDrive[5],thePath[MAX_PATH],theFilename[MAX_PATH],theExtension[MAX_PATH];
_splitpath(theSCTDataFilename,theDrive,thePath,theFilename,theExtension);

您还可以使用类似这样的更通用的标记化函数,该函数接受任何字符串、一个char和一个CStringArray。。

void tokenizeString(CString theString, TCHAR theToken, CStringArray *theParameters)
{
CString temp = "";
int i = 0;
for(i = 0; i < theString.GetLength(); i++ ) 
    {                                   
    if (theString.GetAt(i) != theToken) 
       {
       temp += theString.GetAt(i);  
       }
     else
       {
       theParameters->Add(temp);    
       temp = "";
       }
         if(i == theString.GetLength()-1)
      theParameters->Add(temp);
    }
}
CStringArray thePathParts;
tokenizeString("yourcomplexpathofstringsseparatedbyslashes",'/',&thePathParts);

这将为您提供一个CString数组(CStringArray对象),其中包含输入字符串的每个部分。只要你知道要在上面分割字符串的分隔符,你就可以使用这个函数来解析主要的块,然后再解析次要的块。