0

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 ?

TechBrkTru
  • 346
  • 1
  • 25
  • 2
    Please reduce your code sample to absolute minimum to reproduce the problem and also include any exception stacktrace (if any). Otherwise your question might get closed for being too broad/unclear. – Rosdi Kasim Jul 23 '15 at 08:20
  • Remove `catch { continue; }`. If there are any exceptions they are lost without a trace (eg due to wrong paths, permissions etc). Also, why are you searching for all files instead of the extensions you want? – Panagiotis Kanavos Jul 23 '15 at 08:22
  • 1
    I can read this over and over again and still cannot comprehend what you are talking: `I store in specific folder I get **those file path** but not the **file path of Program Files** inspite of file being **present in C#**` A file being present in C#?? No; it's in your file system. What path to Program Files? Why Program Files? Please be a bit clearer as to what exactly you are trying to achieve and where you fail. – LocEngineer Jul 23 '15 at 08:24
  • 2
    Off topic but I absolute hate when people give booleans generic names like `flag`. What's wrong with something like `skipFilePathCheck`? – DGibbs Jul 23 '15 at 08:24
  • Check this [similar, possibly duplicate question](http://stackoverflow.com/questions/163162/can-you-call-directory-getfiles-with-multiple-filters) for a much simpler solution to searching for multiple extensions – Panagiotis Kanavos Jul 23 '15 at 08:26
  • @PanagiotisKanavos :I have used catch { continue; } inorder to skip the files that have security concerns which doesnt allow to check the files probably the hidden files – TechBrkTru Jul 23 '15 at 08:40
  • 1
    @TechBrkTru then make sure you catch *those* exceptions only, not *all* exceptions. Any other problem will go unnoticed – Panagiotis Kanavos Jul 23 '15 at 08:48
  • I get Unauthorized access exception in Catch due to which it doesnt fetch that particular File example:- Program Files @PanagiotisKanavos – TechBrkTru Jul 23 '15 at 10:15
  • Try to "Run as Administrator". I think you have a standard "Access Denied" issue for "Program Files" folder. – LightBulb Jul 23 '15 at 10:23
  • @LightBulb my .exe ...right ,I set my .exe to those rights yet it didn't fetch – TechBrkTru Jul 23 '15 at 10:31
  • Yes, start your application as administrator. If this is a simple issue of denied access to `Program Files` folder, then that should resolve it. If that is the case, to solve the problem from your program, you need to add a manifest to your project that is going to mark it as requiring elevated access, which, when started, will prompt the user for administrator rights. – LightBulb Jul 23 '15 at 10:35
  • In spite of setting it as "Run as Administrator", I didn't get it . Will it solve by manifest files then what should I add within the project?? @LightBulb – TechBrkTru Jul 23 '15 at 10:42
  • If it doesn't work with "Run as Administrator", then adding a manifest file will not solve it either. I've gone through your code again and I think that your logic is bad. The way you "walk" through directory tree looking for specific file types seems wrong to me. Try to draw a diagram of your process the way it should work and see if that can help you write a better logic. – LightBulb Jul 23 '15 at 10:47
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/84064/discussion-between-techbrktru-and-lightbulb). – TechBrkTru Jul 23 '15 at 10:53
  • What I will do, remove the `try catch...` then re-run the program. Then copy and paste the entire exception stacktrace you are getting here. – Rosdi Kasim Jul 23 '15 at 16:12

0 Answers0