Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get file properties?

I want an application which displays the some file properties of a mediafile if available, like (don't know the exact english words used in windows for it) FileName, Length/Duration, FileType(.avi .mp3 etc.) I tried taglib and windowsapishell but I dont get a working result (references are good)

ShellFile so = ShellFile.FromFilePath(file);
so.Properties.System.(everythingIwant)

shows me a lot of file properties which I want to have displayed, but I cant get it working An example of an error:

'WindowsFormsApplication2.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase\v4.0_4.0.0.0__31bf3856ad364e35\WindowsBase.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled. The program '[6300] WindowsFormsApplication2.vshost.exe: Program Trace' has exited with code 0 (0x0). The program '[6300] WindowsFormsApplication2.vshost.exe: Managed (v4.0.30319)' has exited with code 0 (0x0).

something easy like

var thing = so.Properties.System.FileName.Description;
Console.WriteLine(thing);

wont work

I do know some Java and PHP programming, but I'm totally new to C#


Special thanks to @marr75 and @errorstacks !

one follow up question: I made this, and it works

class Program
{
    static void Main(string[] args)
    {   
        string file = "E:/Dump/Shutter Island.avi";

        FileInfo oFileInfo = new FileInfo(file);
        Console.WriteLine("My File's Name: \"" + oFileInfo.Name + "\"");
        DateTime dtCreationTime = oFileInfo.CreationTime;
        Console.WriteLine("Date and Time File Created: " + dtCreationTime.ToString());
        Console.WriteLine("myFile Extension: " + oFileInfo.Extension);
        Console.WriteLine("myFile total Size: " + oFileInfo.Length.ToString());
        Console.WriteLine("myFile filepath: " + oFileInfo.DirectoryName);
        Console.WriteLine("My File's Full Name: \"" + oFileInfo.FullName + "\"");

    }               
}

but I want it to only provide me with the info if the info exists. I saw the

   **Exists**   Gets a value indicating whether a file exists. (Overrides FileSystemInfo.Exists.)

But how do I use this function, I guess not like if(io.ofileinfo.FullName.exist) {Console.Write(io.ofileinfo.fullname);} ?

like image 976
ComputerIntelligentAgent Avatar asked Oct 22 '11 18:10

ComputerIntelligentAgent


People also ask

How do I get file properties in PowerShell?

To get file attributes in PowerShell, you can use Get-ChildItem or Get-Item cmdlets. It returns the file attributes or properties available on the specified files. To get the list of all properties available, use the Get-Member cmdlet.

How do I find folder properties?

Just as in Windows Explorer, you can view the properties of any file or folder by right-clicking the file, then clicking Properties.

How do I get to file properties in File Explorer?

In File Explorer, hold down the ALT key and simply double click the file or folder. The Properties window will open directly! You don't need to right click and select the Properties menu item. If you prefer exclusively using the keyboard, you can select the file and press Alt+Enter.

What is the properties of a file?

When dealing with files, file properties are pieces of information about that file, which can be accessed via a menu item (often called "Properties"). For example, in Microsoft Windows, you can access the properties of a file by right-clicking the file name and selecting Properties.


1 Answers

When reviewing or opening a file, to get its name, the FileInfo class is equipped with the Name property. Here is an sample code:

FileInfo oFileInfo = new FileInfo(strFilename);

if (oFileInfo != null || oFileInfo.Length == 0)
{
   MessageBox.Show("My File's Name: \"" + oFileInfo.Name + "\"");
   // For calculating the size of files it holds.
   MessageBox.Show("myFile total Size: " + oFileInfo.Length.ToString());
}

You can check like this:

if (!oFileInfo.Exists)
{
    throw new FileNotFoundException("The file was not found.", FileName);
}

To find out what those date and time values are, you can access the File System Information property using:

DateTime dtCreationTime = oFileInfo.CreationTime;
MessageBox.Show("Date and Time File Created: " + dtCreationTime.ToString());

To know the extension of the file, you can access the value of the FileSystemInfo.Extension property:

MessageBox.Show("myFile Extension: " + oFileInfo.Extension);
like image 155
Glory Raj Avatar answered Sep 24 '22 02:09

Glory Raj