473,395 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

FileInfo, Getting File properties, particularly File TYPE

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
May 11 '06 #1
5 3772
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
mike wrote:
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/For...neral/200605/1
May 12 '06 #2
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#
"smc750 via DotNetMonster.com" <u21663@uwe> wrote in message
news:6024efb680013@uwe...
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
mike wrote:
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/For...neral/200605/1

May 12 '06 #3
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

"smc750 via DotNetMonster.com" wrote:
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
mike wrote:
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/For...neral/200605/1

May 12 '06 #4
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;
}
}
}

mike wrote:
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


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...neral/200605/1
May 15 '06 #5
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" wrote:
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;
}
}
}

mike wrote:
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


--
Message posted via DotNetMonster.com
http://www.dotnetmonster.com/Uwe/For...neral/200605/1

May 15 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Antonio | last post by:
Good morning, I've the following file system : C: -> HTML -> Aziende -> Azienda_1 -> a.jpg -> Azienda_2 -> a.jpg ... -> Azienda_N -> a.jpg my desire is to create an array of fileinfo...
5
by: jman | last post by:
I have an urgent question. I have written a .Net C# windows services that has only one key purpose: to move files from the machine that is running the service to a network download server for...
5
by: Lance | last post by:
I want to expose properties of a class to a user via a PropertyGrid class. Some of the properties are of type System.IO.FileInfo. Ideally, an OpenFileDialog window would appear when the user...
7
by: kids_pro | last post by:
I found FileInfo expose alot of methods & properties. However I can't get FileType from FileInfo I can only get File extension. How can I get fileType any build-in library allow me to do that?...
1
by: Tim Failes | last post by:
This seems a trival question, but I cannot get it to work properly... Essentially my question is, how can I create a text file, and guarantee it is given the current date/time as the Creation Time?...
0
by: comp.lang.php | last post by:
if (!function_exists('mime_content_type_fileinfo')) { /** * Will use {@link http://us2.php.net/fileinfo FileInfo} functions provided within {@link http://pecl.php.net PECL} bundle to return mime...
1
by: comp.lang.php | last post by:
<pre> if (!function_exists('mime_content_type_fileinfo')) { /** * Will use {@link http://us2.php.net/fileinfo FileInfo} functions provided within {@link http://pecl.php.net PECL} bundle to return...
5
by: Tom P. | last post by:
I am having the following problem: I create a FileSystemWatcher and wait for events. When the event does happen I refresh a FileSystemInfo list and set properties accordingly (IsFile, IsDir,...
1
by: Tony Johansson | last post by:
Hello! In the FileInfo class there are several properties where you can manipulate the file for example the date and time that the current file was last written to. This sound strange assume...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.