FileInfo, Getting File properties, particularly File TYPE

G

Guest

Hi,
I have been playing with VB.NET/C# for getting some general properties of a
fileinfo object. However, FileInfo object does not seem to expose some of the
basic properties like TYPE that used to be available in "FileSystemObject" of
ScriptingLibrary. For example the "TYPE" property should return "Text
Document" or "Microsoft Office Document" or "Visual Studio Code File" and so
on. similar to the properties displayed by Properties DialogBox when we
right-click and select Properties From Context-Menu in Windows Explorer.

I was able to get general properties like Modified date, attributes like
ReadOnly, Archieve, Hidden. but after spending hours I resorted to posting
this here.
Any help is greatly appreciated.
Thanks
--Mayur Hirpara
 
S

smc750 via DotNetMonster.com

A lot of this information is in the registry. For instance, the 'TYPE' you
refer to is set by the application that is associated. You can get the file
type description via the following code. For example, if you pass in the
extension of .tif, based on what application is associated with the extension,
it will return something like
"MSPaper.Document". Try the following code. You could create a class that
could encapsulate all the properties you want to get for a file via the
registry and then use this in your code to augment the fileInfo class.
Remember also that your code will need registry permissions to access the
registry which can be set via an attribute at the top of the class.

using System;
using Microsoft.Win32;


namespace CertDev
{

public class RegHelper
{
public RegHelper()
{}

public string GetFileType(string extension)
{

string contentType;

using(RegistryKey rgk = Registry.ClassesRoot.OpenSubKey("\\"+extension))
{

contentType = rgk.GetValue("",string.Empty).ToString();

}

return contentType;


}
}
}

smc750
www.certdev.com

Hi,
I have been playing with VB.NET/C# for getting some general properties of a
fileinfo object. However, FileInfo object does not seem to expose some of the
basic properties like TYPE that used to be available in "FileSystemObject" of
ScriptingLibrary. For example the "TYPE" property should return "Text
Document" or "Microsoft Office Document" or "Visual Studio Code File" and so
on. similar to the properties displayed by Properties DialogBox when we
right-click and select Properties From Context-Menu in Windows Explorer.

I was able to get general properties like Modified date, attributes like
ReadOnly, Archieve, Hidden. but after spending hours I resorted to posting
this here.
Any help is greatly appreciated.
Thanks
--Mayur Hirpara

--
smc750
www.certdev.com

Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/Forums.aspx/dotnet-general/200605/1
 
G

Greg Young

Just to add a bit to this .. I would imagine the reason why it is not there
is that these are classes from the BCL which are supposed to be system
agnostic (see ECMA 335 partition 4). Since many other systems do not know
the concept of this description string it would not make alot of sense to
put it in.

Cheers,

Greg Young
MVP - C#
 
G

Guest

Hi smc750,
your solution is closer to what I am asking for.
It does give me Registrykey, but I am actually looking for "TYPE" of the
file which is displayed in Windows Explorer.
For Example "Text Document" or "Microsoft Office Document" and so on.
It was possible to get it straightly using "FileSystemObject" available as a
part of Scripting library for Classic ASP development. However in .NET I do
not want to use it. I just want to stay with .NET class Library.
Thanks
--Mayur
 
S

smc750 via DotNetMonster.com

Sorry for the delay on answering.

I modified the code. The new code now makes a second lookup into the registry.
The initial lookup actually returns the application class that the file
extension is associated with. You can use this return value to lookup into
the classesroot hive and get the friendly type name you are looking for. This
will only work if there is an application associated with the file extension.
If there is not one it will return an empty string.


Hope this helps,

smc750
www.certdev.com

using System;
using Microsoft.Win32;

namespace CertDev
{

public class RegHelper
{
public RegHelper()
{}

public string GetFileType(string extension)
{

string applicationType;
string contentType;


//get the application class the extension is associated to
using(RegistryKey rgk = Registry.ClassesRoot.OpenSubKey("\\"+extension))
{

applicationType = rgk.GetValue("",string.Empty).ToString();

}

//get the file type description for the application associated to
using(RegistryKey rgk = Registry.ClassesRoot.OpenSubKey("\\" +
applicationType))
{
contentType = rgk.GetValue("",string.Empty).ToString();

}

return contentType;


}
}
}
Hi smc750,
your solution is closer to what I am asking for.
It does give me Registrykey, but I am actually looking for "TYPE" of the
file which is displayed in Windows Explorer.
For Example "Text Document" or "Microsoft Office Document" and so on.
It was possible to get it straightly using "FileSystemObject" available as a
part of Scripting library for Classic ASP development. However in .NET I do
not want to use it. I just want to stay with .NET class Library.
Thanks
--Mayur
A lot of this information is in the registry. For instance, the 'TYPE' you
refer to is set by the application that is associated. You can get the file
[quoted text clipped - 55 lines]
 
G

Guest

Hi smc750,
Excellent feedback. I love this solution.
I am really happy that someone had it in the bank.
Thanks
--Mayur

smc750 via DotNetMonster.com said:
Sorry for the delay on answering.

I modified the code. The new code now makes a second lookup into the registry.
The initial lookup actually returns the application class that the file
extension is associated with. You can use this return value to lookup into
the classesroot hive and get the friendly type name you are looking for. This
will only work if there is an application associated with the file extension.
If there is not one it will return an empty string.


Hope this helps,

smc750
www.certdev.com

using System;
using Microsoft.Win32;

namespace CertDev
{

public class RegHelper
{
public RegHelper()
{}

public string GetFileType(string extension)
{

string applicationType;
string contentType;


//get the application class the extension is associated to
using(RegistryKey rgk = Registry.ClassesRoot.OpenSubKey("\\"+extension))
{

applicationType = rgk.GetValue("",string.Empty).ToString();

}

//get the file type description for the application associated to
using(RegistryKey rgk = Registry.ClassesRoot.OpenSubKey("\\" +
applicationType))
{
contentType = rgk.GetValue("",string.Empty).ToString();

}

return contentType;


}
}
}
Hi smc750,
your solution is closer to what I am asking for.
It does give me Registrykey, but I am actually looking for "TYPE" of the
file which is displayed in Windows Explorer.
For Example "Text Document" or "Microsoft Office Document" and so on.
It was possible to get it straightly using "FileSystemObject" available as a
part of Scripting library for Classic ASP development. However in .NET I do
not want to use it. I just want to stay with .NET class Library.
Thanks
--Mayur
A lot of this information is in the registry. For instance, the 'TYPE' you
refer to is set by the application that is associated. You can get the file
[quoted text clipped - 55 lines]
Thanks
--Mayur Hirpara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top