Click here to Skip to main content
15,901,122 members
Home / Discussions / C#
   

C#

 
AnswerRe: a WinForm ToolStripMenuItem quirk ? Pin
Richard Deeming22-Oct-19 0:41
mveRichard Deeming22-Oct-19 0:41 
GeneralRe: a WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff22-Oct-19 0:48
professionalBillWoodruff22-Oct-19 0:48 
GeneralRe: a WinForm ToolStripMenuItem quirk ? Pin
Richard Deeming22-Oct-19 1:07
mveRichard Deeming22-Oct-19 1:07 
GeneralRe: a WinForm ToolStripMenuItem quirk ? Pin
BillWoodruff22-Oct-19 19:04
professionalBillWoodruff22-Oct-19 19:04 
Question'Access to the path 'F:\System Volume Information' is denied.' Pin
Member 1405587919-Oct-19 8:45
Member 1405587919-Oct-19 8:45 
AnswerRe: 'Access to the path 'F:\System Volume Information' is denied.' Pin
Eddy Vluggen19-Oct-19 11:11
professionalEddy Vluggen19-Oct-19 11:11 
GeneralRe: 'Access to the path 'F:\System Volume Information' is denied.' Pin
Member 1405587920-Oct-19 2:43
Member 1405587920-Oct-19 2:43 
GeneralRe: 'Access to the path 'F:\System Volume Information' is denied.' Pin
Eddy Vluggen20-Oct-19 4:53
professionalEddy Vluggen20-Oct-19 4:53 
Member 14055879 wrote:
exception handler does not work.
It does; since "does not work" can mean anything, I tested it. The framework throws an exception as soon as GetFiles() fails. Meaning that if you try to "GetFiles" on something you don't have access to, it will throw an exception and not return any results.

Member 14055879 wrote:
And I don't know how to use object.ForEach.
You are already using that; there's a simpeler foreach loop that is almost always more appropriate.

Member 14055879 wrote:
Is it possible in this case to use LINQ?
No, since the exception will throw in the same place (Directory.GetFiles), and it wouldn't return a result. So filtering that result will still yield nothing.

Simple solution; write your own GetFiles that skips those and that returns the rest as the result. Code below been tested on my machine. Do remember that getting all files and folders from a drive will take "some time" - this might best be done from a separate thread, updating the UI as items are returned.

C#
public Form1()
{
    InitializeComponent();
    string[] originalFiles = GetNonSystemFiles(@"C:\");
    foreach (string originalFileLocation in originalFiles)
    {
        // stuff
    }
}

string[] GetNonSystemFiles(string path)
{
    DirectoryInfo di = new DirectoryInfo(path);

    // reserve list for result
    List<string> result = new List<string>();

    // get folders
    IEnumerable<DirectoryInfo> folders = null;
    try
    {
        folders = di.EnumerateDirectories("*", SearchOption.TopDirectoryOnly);
    }
    catch (System.UnauthorizedAccessException uax)
    {
        System.Diagnostics.Debug.WriteLine("Unauthorized folder for: {0}, ex: {1}", di.Name, uax.Message);
    }

    if (null != folders)
        foreach (DirectoryInfo folder in folders)
        {
            result.Add(folder.FullName);

            // get this folders' contents and add it to the result too
            if ((folder.Attributes & FileAttributes.System) != FileAttributes.System // skip system folders
            && (folder.Attributes & FileAttributes.Hidden) != FileAttributes.Hidden) // skip hidden folders
                result.AddRange(GetNonSystemFiles(folder.FullName).ToList());
        }

    // get files for this folder
    IEnumerable<FileInfo> fileInfos = null;
    try
    {
        fileInfos = di.EnumerateFiles("*", SearchOption.TopDirectoryOnly);
    }
    catch (UnauthorizedAccessException uax)
    {
        System.Diagnostics.Debug.WriteLine("Unauthorized file for: {0}, ex: {1}", di.Name, uax.Message);
    }
    if (fileInfos != null)
        foreach (FileInfo fileInfo in fileInfos)
        {
            result.Add(fileInfo.FullName);
        }

    return result.ToArray();
}


Bastard Programmer from Hell Suspicious | :suss:
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.

GeneralRe: 'Access to the path 'F:\System Volume Information' is denied.' Pin
Dave Kreskowiak20-Oct-19 4:59
mveDave Kreskowiak20-Oct-19 4:59 
Questionis any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
BillWoodruff18-Oct-19 2:05
professionalBillWoodruff18-Oct-19 2:05 
AnswerRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Richard Deeming18-Oct-19 2:17
mveRichard Deeming18-Oct-19 2:17 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Luc Pattyn18-Oct-19 3:45
sitebuilderLuc Pattyn18-Oct-19 3:45 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Richard Deeming18-Oct-19 3:56
mveRichard Deeming18-Oct-19 3:56 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Luc Pattyn18-Oct-19 4:05
sitebuilderLuc Pattyn18-Oct-19 4:05 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
BillWoodruff18-Oct-19 4:09
professionalBillWoodruff18-Oct-19 4:09 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Luc Pattyn18-Oct-19 4:19
sitebuilderLuc Pattyn18-Oct-19 4:19 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
BillWoodruff18-Oct-19 3:54
professionalBillWoodruff18-Oct-19 3:54 
AnswerRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
Bernhard Hiller20-Oct-19 22:35
Bernhard Hiller20-Oct-19 22:35 
GeneralRe: is any threat (as in sql injection) possible in building a 'RowFilter for a 'DataView Pin
BillWoodruff21-Oct-19 1:01
professionalBillWoodruff21-Oct-19 1:01 
QuestionCan I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846717-Oct-19 17:42
Member 245846717-Oct-19 17:42 
AnswerRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
OriginalGriff17-Oct-19 20:09
mveOriginalGriff17-Oct-19 20:09 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846717-Oct-19 22:14
Member 245846717-Oct-19 22:14 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
OriginalGriff17-Oct-19 22:36
mveOriginalGriff17-Oct-19 22:36 
GeneralRe: Can I convert EventArgs type to PaintEventArgs type ? Pin
Member 245846721-Oct-19 17:03
Member 245846721-Oct-19 17:03 
QuestionUpdating a List record within a record with c# and Linq, looking for something cleaner than add and remove Pin
jkirkerx17-Oct-19 12:50
professionaljkirkerx17-Oct-19 12:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.