Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get file list and information using .NET 1

Status
Not open for further replies.

randall2nd

Programmer
May 30, 2001
112
US
I am new to C# and .NET and I am tring to display a listing of files and some information on each file from a specified directory.

I need the file name, create date, modified date, and owner name. The first three are easy, its the last that I have no clue how to get within the .NET frame work. I know I can get it by using methods outside .NET, but I need to know if its possible and how to get it using .NET.

I am currently using the DirectoryInfo object and looping over the results from the GetFileSystemInfos() method. In the loop I am grabbing the five pieces of data needed and putting them into a ListViewItem as a string[].

Is there a more effeceint method?

Any help/advice would be appreciated.
Most effeceint way to display info?
How do I get the owner of a file?


Randall2nd
 
Randall2nd -
This is quite easy under the framework. Add a "using System.IO;" to the top of your source file, then in a method use the DirectoryInfo object. Here's some sample code from MSDN:
Code:
  // Create a reference to the current directory.
  DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory);
  // Create an array representing the files in the current directory.
  FileInfo[] fi = di.GetFiles();
  Console.WriteLine("The following files exist in the current directory:");
  // Print out the names of the files in the current directory.
  foreach (FileInfo fiTemp in fi)
    Console.WriteLine(fiTemp.Name);
Once you have a FileInfo object, you can use it's methods to get creationtime, size (length), etc.

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Yea, geting most of the data was easy. Its the "OWNER" field that I am having trouble with.

The "OWNER" field I am refering to is the one that displays in windows explorer in the details view.

Where is the "OWNER" field at?
How do I access the "OWNER" field?

Randall2nd
 
That field is generated by a Windows Shell extension that looks at the security info for the file. Right now I'm drawing a blank as to what the API functions are called that will get it for you (It's not in the .NET framework).

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Hey, Randall2nd!

Here is a part of a code i wrote for open list of files of some current folder. There are no information about each file, but i think it is no impossible to get it. If you want i can send you hole application.

string strPath;
protected System.Web.UI.WebControls.Literal litNoneSelected;
public string[] strFiles;

private void Page_Load(object sender, System.EventArgs e)
{
//get the path to the file
strPath = Session["Path"].ToString();
//if the event doesn't initiate page postback...
if (!Page.IsPostBack)
{
//get the list of files in current folder
strFiles = Directory.GetFiles(strPath);
//get names of files
for (int iCount = 0; iCount <= strFiles.GetUpperBound(0); iCount++)
{
strFiles[iCount] = Path.GetFileName(strFiles[iCount]);
}
}
//bind lstFiles to array with names of files
lstFiles.DataBind();
}


*********************
lstFiles - listBox's name

Hope it will help!
Best regards,
Alexander
 
chiph: I was affraid of that. My directive is stay strictly inside .NET framework.

Thanks for the assistance. :)

Randall2nd
 
Figured it out!!!! (sort of)

Not exactly what I was looking for. It hits against the Win 32 stuff, but the help files say they are in the .NET framework.

Here is what worked for me.

Code:
    private void Retrieve_Files(String sCurrentDirectory, FileInfoExtCollection CurFileInfoExtList)
    //private void Retrieve_Files(String sCurrentDirectory)
    {
      ManagementObject moFileSecurity;
      ManagementBaseObject outParam;
      ManagementBaseObject mboFileSecurityDescriptor;
      ManagementBaseObject mboFileTrustee;
      String sSearchPattern = &quot;*.*&quot;;
      String sFilter = &quot;&quot;;

      //get file list
      DirectoryInfo CurDirInfo = new DirectoryInfo(sCurrentDirectory);
      if (CurDirInfo.Exists)
      { 
        try
        {
          //ManagementObject moFileOwner = new ManagementObject(&quot;Win32_LogicalFileOwner&quot;);
          foreach(FileInfo CurFileInfo in CurDirInfo.GetFiles(sSearchPattern))
          {
            //check if filter should be applied
            if((sFilter == String.Empty)
              ||(sFilter != String.Empty && CurFileInfo.Name.IndexOf(sFilter) != -1))
            {
              FileInfoExt tempFileInfoExt = new FileInfoExt(CurFileInfo);
              tempFileInfoExt.Owner    = DefaultOwner;  //load DefaultOwner

              if(ListFileOwners)
              {
                //get actual owner
                try
                {
                  moFileSecurity = new ManagementObject(&quot;Win32_LogicalFileSecuritySetting.path='&quot; + CurFileInfo.FullName + &quot;'&quot;);
                  outParam = moFileSecurity.InvokeMethod(&quot;GetSecurityDescriptor&quot;,moFileSecurity,null);
 
                  if(outParam.GetPropertyValue(&quot;returnValue&quot;).ToString() == &quot;0&quot;)
                  {
                    mboFileSecurityDescriptor = outParam.GetPropertyValue(&quot;Descriptor&quot;) as ManagementBaseObject;
                    mboFileTrustee = mboFileSecurityDescriptor.GetPropertyValue(&quot;Owner&quot;) as ManagementBaseObject;
                    tempFileInfoExt.Owner = mboFileTrustee.GetPropertyValue(&quot;Name&quot;).ToString();
                  }
                }
                catch
                {
                  tempFileInfoExt.Owner    = DefaultOwner;
                }
              }

              CurFileInfoExtList.Add(tempFileInfoExt);
            }//end if -- filter check
          }//end foreach
        }//end try
        catch (Exception e)
        {
          Show_Error(e);
        }
      }//end if --Directory exists
    }

If you can think of a better way to do it, PLEASE post back, this way takes forever (hence reason I made an option to turn it off).

Randall2nd
 
I never thought to look at WMI. It's not something that I would think to contain security descriptors.

And, yeah, WMI is very slow. ;-)

Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top