locked
How to Find hidden files

    Question

  • I m developing an application in which my requirement is that the program will scan the files and user will select the files to delete them.But the question is that How i can determine that 

           The file is Hidden or Not 

           The file is being used by other programs

     Because if the file is being used by other programs that i can generate inconsistent results.May also generate errors. So please guide me how can i handle it.

    Tuesday, December 29, 2009 10:33 AM

Answers

  • Hi,

    To find out if file is hidden, you need to get file's attributes. Bellow are two examples of how to determine if file is hidden.
    http://www.csharp-examples.net/file-attributes/
    http://msdn.microsoft.com/en-us/library/system.io.file.getattributes.aspx

    And here's an example of how to do it with FileInfo:

    DirectoryInfo di = new DirectoryInfo("C:\\");
    
    foreach (FileInfo fileInfo in di.GetFiles("*.*"))
    {
        bool isHidden = (fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
    
        if (isHidden)
        {
            Console.WriteLine("File \"{0}\" is hidden", fileInfo.FullName);
        }
    }

    The second answer is not so relevant as first. I thinks it's the best if you take a look at this article:
    http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx


    Regards,
    Danijel

    Blog, Twitter
    • Proposed as answer by Narayanan Dayalan Tuesday, December 29, 2009 12:04 PM
    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 11:30 AM
  • To check for Hidden file you can use following code

              System.IO.FileAttributes fa = System.IO.File.GetAttributes("File Path");
                if ((fa & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
                {
                    MessageBox.Show("File is hidden");
                }
    To check whether the file is used by another process you can try following code

             private bool IsOpened(string path)
            {
                try
                {
                    System.IO.File.OpenWrite(path);
                    return true;
                }
                catch (System.UnauthorizedAccessException ex)
                {
                    return false;
                }
            }



    Gaurav Khanna
    • Proposed as answer by Narayanan Dayalan Tuesday, December 29, 2009 12:04 PM
    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 11:38 AM
  • Use try/catch to check if the file is in use when you want to delete it, not before.
    Another program could open the file after you check it and before you try to delete it.

    If you really want, you can also check it before, but I don't think it adds anything.

    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 10:34 PM

All replies

  • Hi,

    To find out if file is hidden, you need to get file's attributes. Bellow are two examples of how to determine if file is hidden.
    http://www.csharp-examples.net/file-attributes/
    http://msdn.microsoft.com/en-us/library/system.io.file.getattributes.aspx

    And here's an example of how to do it with FileInfo:

    DirectoryInfo di = new DirectoryInfo("C:\\");
    
    foreach (FileInfo fileInfo in di.GetFiles("*.*"))
    {
        bool isHidden = (fileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
    
        if (isHidden)
        {
            Console.WriteLine("File \"{0}\" is hidden", fileInfo.FullName);
        }
    }

    The second answer is not so relevant as first. I thinks it's the best if you take a look at this article:
    http://www.codeproject.com/KB/shell/OpenedFileFinder.aspx


    Regards,
    Danijel

    Blog, Twitter
    • Proposed as answer by Narayanan Dayalan Tuesday, December 29, 2009 12:04 PM
    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 11:30 AM
  • To check for Hidden file you can use following code

              System.IO.FileAttributes fa = System.IO.File.GetAttributes("File Path");
                if ((fa & System.IO.FileAttributes.Hidden) == System.IO.FileAttributes.Hidden)
                {
                    MessageBox.Show("File is hidden");
                }
    To check whether the file is used by another process you can try following code

             private bool IsOpened(string path)
            {
                try
                {
                    System.IO.File.OpenWrite(path);
                    return true;
                }
                catch (System.UnauthorizedAccessException ex)
                {
                    return false;
                }
            }



    Gaurav Khanna
    • Proposed as answer by Narayanan Dayalan Tuesday, December 29, 2009 12:04 PM
    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 11:38 AM
  • Use try/catch to check if the file is in use when you want to delete it, not before.
    Another program could open the file after you check it and before you try to delete it.

    If you really want, you can also check it before, but I don't think it adds anything.

    • Marked as answer by Bin-ze Zhao Wednesday, December 30, 2009 8:55 AM
    Tuesday, December 29, 2009 10:34 PM
  • ok thanks sir, i have got the solution now.

    But i am facing another problem regarding directory permissions. when the program tries to explore the directories which don't haveread write permissions then it throws exception. so how i can handle this problem.
    Monday, January 11, 2010 1:14 PM