简体   繁体   English

使用Directory.GetFiles访问被拒绝的异常时,如何传递文件夹或文件?

[英]How do i pass over a folder/s or file/s when using Directory.GetFiles access denied exception?

List<string> alltypes = new List<string>();
try
{
    alltypes = Directory.GetFiles(textBox3.Text, "*.*", SearchOption.AllDirectories).ToList();
}
catch (UnauthorizedAccessException)
{

}
if (alltypes.Count > 0)
{
    if (comboBox1.Items.Count > 0)
        comboBox1.Items.Clear();
    comboBox1.Items.Add("Possible Extensions");
    foreach (string ext in alltypes)
    {
        if (!comboBox1.Items.Contains(Path.GetExtension(ext)))
            comboBox1.Items.Add(Path.GetExtension(ext));
    }    
}

When it gets to the catch it just stops, it never continues getting the files so the List alltypes is empty. 当它到达catch它只是停止,它永远不会继续获取文件,这样的List alltypes是空的。 For example in this case in textBox3.Text the value is c:\\ . 例如,在这种情况下,在textBox3.Text ,值为c:\\

It depends on the rights needed to access the folder. 这取决于访问文件夹所需的权限。 There are too many options. 有太多选择。 For example it could be set by an active directory. 例如,它可以由活动目录设置。 Output your exception in the catch block an check the details. 在catch块中输出您的异常,然后检查详细信息。

But in general it can be broken down to: How to request administrator permissions when the program starts? 但通常可以将其分解为: 程序启动时如何请求管理员权限?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM