How to get File Path from C:\Program Files Folder

2019-09-11 06:59发布

Following code splits the file extension and puts period operator that finds for the respective extension files

extensionNames = mediafilesExtensionNames.Split('|');                           
for (int i = 0; i < extensionNames.Length; i++)
{
    var regexItem = new Regex("^[a-zA-Z0-9]*$");
    if (!regexItem.IsMatch(extensionNames[i])||extensionNames[i]=="" || (extensionNames[i].Contains(" ")==true))
    {
        media_files_path ="Unknown";
        skipFilePathCheck= false; //setting it as false so that it skips the checking of file paths
        break;
    }
    else if (!extensionNames[i].StartsWith("."))
    {
        extensionNames[i] = '.' + extensionNames[i];
        skipFilePathCheck= true;
    }
}
if (skipFilePathCheck == true)
{
    GetFiles(path); //path is "C:\"
}

Later after the files are splitted I search for specific Files types.

List<string> filesFound = new List<string>();
void GetFiles(string sDir)
{
    foreach (string directory in Directory.GetDirectories(sDir))
    {
        try
        {
            foreach (string files in Directory.GetFiles(directory, "*.*"))
            {
                if (extensionNames.Any(files.Contains))
                {
                    filesFound.Add(directory);
                    media_files_path += directory + "?";
                }
                break;//to check for next for next folder
            }
            GetFiles(directory); //to check for the nested directories
        }
        catch
        {
            continue; //to avoid the unauthorized access denied files
        }
    } //foreach exit
} //else loop exit

Even If I find a single file I take the path and skip the folder. But the problem is If saved a file for eg: mp3 in specific folder I get those file path ,but not the file path of Program Files in spite of file being present in C:\Program Files.It doesnt check this type of Files due to Unauthorized Exception Access Denied .How could I solve this ?

0条回答
登录 后发表回答