c#中的directoryinfo类根本无法访问该文件夹。 [英] The folder won't be accessible at all with directoryinfo class in c#.

查看:62
本文介绍了c#中的directoryinfo类根本无法访问该文件夹。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我查找文件夹时,C:\ Windows\System32 \ WindowsPowerShell \v1.0 \ Modules \ AppBackgroundTask,ListViewbox没有文件,错误信息是找不到部分文件路径'C:\ Windows \ System32 \ WindowsPowerShell \v1.0 \ Module'\\ AppBackgroundTask'。该文件夹已存在。我们如何获取文件夹中的文件?



  private  < span class =code-keyword> void  cmdList_Click( object  sender,EventArgs e)
{

< span class =code-comment> // '********************
// '此部分不需要列出文件,只需准备好LISTVIEW。
lvwFiles.View = View.Details; // 'SET REPORT STYLE

lvwFiles.Items.Clear(); // '从上一个时间开始清除
lvwFiles.Columns.Clear();

lvwFiles.Columns.Add( Filename 100 ,Horizo​​ntalAlignment.Left); // '在运行时创建字符
lvwFiles.Columns.Add( Extension 45 ,Horizo​​ntalAlignment.Center);
lvwFiles.Columns.Add( Size(KB) 50 ,Horizo​​ntalAlignment.Center);
lvwFiles.Columns.Add( 创建时间 120 ,Horizo​​ntalAlignment.Center);
// '******************* **

// Dim dFolder As DirectoryInfo = New DirectoryInfo(txtFolder。文本)
// Dim fFileArray()As FileInfo = dFolder.GetFiles
DirectoryInfo dFolder = new DirectoryInfo(txtFolder.Text);
尝试
{
FileInfo [] fFileArray = dFolder.GetFiles();
// string [] FileInfo = dFolder .GetFiles();
< span class =code-comment> // string [] fFileArray;
// FileInfo = As FileInfo = dFolder.GetFiles
// 'FILEARRAY'现在包含所选文件夹中的所有文件
FileInfo fFile;
ListViewItem lCurrent;

// Dim fFile As FileInfo
// Dim lCurrent As ListViewItem

// '通过阵列循环,列出LISTVIEW中的所有文件
// For FFile in fFileArray
// lCurrent = lvwFiles .Items.Add(fFile.Name)
// lCurrent.SubItems.Add(fFile .Extension)
// lCurrent.SubItems.Add(fFile.Length \ 1024 + 1)//'转换为KB的字节
// lCurrent.SubItems.Add(fFile.CreationTime)
// 下一步

foreach (FileInfo f in fFileArray)
{
lCurrent = lvwFiles.Items.Add(f.Name);
lCurrent.SubItems.Add(f.Extension);
lCurrent.SubItems.Add((f.Length / 1024 + 1 )。ToString( )); // 'CONVERT BYTES TO KB
lCurrent.SubItems.Add(f.CreationTime.ToString ());
}
}
catch (例外情况)
{
Console.WriteLine(ex.Message) ;
}





我尝试过:



我的帐户是管理员权限之一。当我的帐户在运行dir之前使用dir / a:d时,无法显示该文件夹,但在运行dir / a:d后,该文件夹将显示在路径中。

解决方案

文件夹路径是一个好路径。问题是该文件夹以及Modules文件夹中的其他16个文件夹都设置了System属性。这使得它们有点......安全...反对DirectoryInfo类的GetDirectories和GetFiles方法的安全检查。



你可以看看DirectoryInfo类的源代码,此处 [ ^ ],但这不是一个简单的阅读,可能不会除了在创建DirectoryInfo类的实例时针对您提供的路径进行了大量安全检查之外,请告诉您。



看来,即使以管理员身份运行代码,代码也会以某种方式失败安全检查。



我现在没时间自己挖掘它。

When I look up the folder, C:\Windows\System32\WindowsPowerShell\v1.0\Modules\AppBackgroundTask, ListViewbox has no files and the error message is "Could not find a part of the path 'C:\Windows\System32\WindowsPowerShell\v1.0\Modules\AppBackgroundTask'." The folder is existed. How can we get the files in the folder?

private void cmdList_Click(object sender, EventArgs e)
       {

       //' ********************
       //'THIS SECTION IS NOT REQUIRED TO LIST FILES, IT JUST PREPARES THE LISTVIEW.
       lvwFiles.View = View.Details; //'SET REPORT STYLE

       lvwFiles.Items.Clear(); //'CLEAR ALL FROM PREVIOUS TIME
       lvwFiles.Columns.Clear();

       lvwFiles.Columns.Add("Filename", 100, HorizontalAlignment.Left) ;//'CREATE COLOUMS AT RUN TIME
       lvwFiles.Columns.Add("Extension", 45, HorizontalAlignment.Center);
       lvwFiles.Columns.Add("Size (KB)", 50, HorizontalAlignment.Center);
       lvwFiles.Columns.Add("Create Time", 120, HorizontalAlignment.Center);
       //' *********************

       //Dim dFolder As DirectoryInfo = New DirectoryInfo(txtFolder.Text)
       //Dim fFileArray() As FileInfo = dFolder.GetFiles
           DirectoryInfo dFolder = new DirectoryInfo (txtFolder.Text);
           try
           {
                FileInfo[] fFileArray = dFolder.GetFiles();
                //string[] FileInfo = dFolder .GetFiles ();
                //string[] fFileArray;
                //FileInfo = As FileInfo = dFolder.GetFiles
                //' 'FILEARRAY' NOW HOLDS ALL THE FILES IN THE SELECTED FOLDER
                FileInfo fFile;
                ListViewItem lCurrent;

                //Dim fFile As FileInfo
                //Dim lCurrent As ListViewItem

                // ' LOOP THROUGH ARRAY, LISTING ALL FILES IN LISTVIEW
                //For Each fFile In fFileArray
                //    lCurrent = lvwFiles.Items.Add(fFile.Name)
                //    lCurrent.SubItems.Add(fFile.Extension)
                //    lCurrent.SubItems.Add(fFile.Length \ 1024 + 1) //'CONVERT BYTES TO KB
                //    lCurrent.SubItems.Add(fFile.CreationTime)
                //Next

                foreach (FileInfo f in fFileArray)
                {
                    lCurrent = lvwFiles.Items.Add(f.Name);
                    lCurrent.SubItems.Add(f.Extension);
                    lCurrent.SubItems.Add((f.Length / 1024 + 1).ToString()); //'CONVERT BYTES TO KB
                    lCurrent.SubItems.Add(f.CreationTime.ToString());
                }
           }
           catch(Exception ex)
           {
               Console.WriteLine(ex.Message);
          }



What I have tried:

My account is one of the admin privilege. When my account use dir /a:d before running dir, the folder can't be shown, but after running dir /a:d, the folder is displayed in the path.

解决方案

The folder path is a good path. The problem is that the folder, along with 16 others in the Modules folder, have the System attribute set on them. This makes them a little ... secure ... against the security checks of the GetDirectories and GetFiles methods of the DirectoryInfo class.

You could take a look at the source code for the DirectoryInfo class, here[^], but it's not an easy read and probably won't tell you much, other than there is a bunch of security checks done against the path you supplied when you create an instance of the DirectoryInfo class.

It would appear that the code is failing the security checks somehow, even while running the code as an Administrator.

I don't have time to dig through it myself right now.


这篇关于c#中的directoryinfo类根本无法访问该文件夹。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆