Forum     

Go Back   Digit Technology Discussion Forum > Portables, Peripherals and Electronics > QnA (read only)
Register FAQ Calendar Mark Forums Read

QnA (read only) Mods please help transfer the contents of this forum to proper sections. :)

 
 
LinkBack (5) Thread Tools Search this Thread Display Modes
Old 16-06-2006, 03:22 PM   5 links from elsewhere to this Post. Click to view. #1 (permalink)
The Frozen Nova
 
casanova's Avatar
 
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
Default C#.net how to access properties of a file??

When we right click some file and then click properties, the properties page is displayed. In this property window there, is a summary tab. I wanted to know how to modify the details present in the summary tab like title,subject,keywords etc through C#.net

These are called id3 tags. So, if you have any idea, plz post.
__________________
I dream of a better tomorrow... where chickens can cross roads and not have their motives questioned.

www.nerdweed.blogspot.com
casanova is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 16-06-2006, 03:53 PM   #2 (permalink)
C# Be Sharp !
 
Zeeshan Quireshi's Avatar
 
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
Default Re: C#.net how to access properties of a file??

well here u got , don't gorget ro rep my if u find my post useful !

Code:
using System;
using System.IO;
using System.Text;

namespace mp3info
{

	public class ID3v1
	{
		public string filename;

		public string Title;
		public string Artist;
		public string Album;
		public string Year;
		public string Comment;
		public int GenreID;
		public int Track;

		public bool hasTag;

		private void Initialize_Components()
		{
			hasTag = false;
			filename = "";
			Title = "";
			Artist = "";
			Album = "";
			Year = "";
			Comment = "";

			GenreID = 0;
			Track = 0;
		}


		public ID3v1()
		{
			Initialize_Components();
		}
		
		public ID3v1( string filename )
		{
			Initialize_Components();
			this.filename = filename;
		}


		public void Read () 
		{
			// Read the 128 byte ID3 tag into a byte array
			FileStream oFileStream;
			oFileStream = new FileStream( this.filename, FileMode.Open);
			byte[] bBuffer = new byte[128];
			oFileStream.Seek(-128, SeekOrigin.End);
			oFileStream.Read(bBuffer,0, 128);
			oFileStream.Close();
  
			// Convert the Byte Array to a String
			Encoding  instEncoding = new ASCIIEncoding();   // NB: Encoding is an Abstract class
			string id3Tag = instEncoding.GetString(bBuffer);
  
			// If there is an attched ID3 v1.x TAG then read it 
			if (id3Tag .Substring(0,3) == "TAG") 
			{
				this.Title      = id3Tag.Substring(  3, 30).Trim();
				this.Artist     = id3Tag.Substring( 33, 30).Trim();
				this.Album      = id3Tag.Substring( 63, 30).Trim();
				this.Year     = id3Tag.Substring( 93, 4).Trim();
				this.Comment    = id3Tag.Substring( 97,28).Trim();
  
				// Get the track number if TAG conforms to ID3 v1.1
				if (id3Tag[125]==0)
					this.Track = bBuffer[126];
				else
					this.Track = 0;
				this.GenreID = bBuffer[127];

				this.hasTag    = true;
				// ********* IF USED IN ANGER: ENSURE to test for non-numeric year
			}
			else 
			{
				this.hasTag      = false;
			}
		}
  
		public void updateMP3Tag () 
		{
			// Trim any whitespace
			this.Title = this.Title.Trim();
			this.Artist = this.Artist.Trim();
			this.Album = this.Album.Trim();
			this.Year   = this.Year.Trim();
			this.Comment = this.Comment.Trim();
  
			// Ensure all properties are correct size
			if (this.Title.Length > 30)   this.Title    = this.Title.Substring(0,30);
			if (this.Artist.Length > 30)  this.Artist   = this.Artist.Substring(0,30);
			if (this.Album.Length > 30)   this.Album    = this.Album.Substring(0,30);
			if (this.Year.Length > 4)     this.Year     = this.Year.Substring(0,4);
			if (this.Comment.Length > 28) this.Comment  = this.Comment.Substring(0,28);
      
			// Build a new ID3 Tag (128 Bytes)
			byte[] tagByteArray = new byte[128];
			for ( int i = 0; i < tagByteArray.Length; i++ ) tagByteArray[i] = 0; // Initialise array to nulls
  
			// Convert the Byte Array to a String
			Encoding  instEncoding = new ASCIIEncoding();   // NB: Encoding is an Abstract class // ************ To DO: Make a shared instance of ASCIIEncoding so we don't keep creating/destroying it
			// Copy "TAG" to Array
			byte[] workingByteArray = instEncoding.GetBytes("TAG"); 
			Array.Copy(workingByteArray, 0, tagByteArray, 0, workingByteArray.Length);
			// Copy Title to Array
			workingByteArray = instEncoding.GetBytes(this.Title);
			Array.Copy(workingByteArray, 0, tagByteArray, 3, workingByteArray.Length);
			// Copy Artist to Array
			workingByteArray = instEncoding.GetBytes(this.Artist);
			Array.Copy(workingByteArray, 0, tagByteArray, 33, workingByteArray.Length);
			// Copy Album to Array
			workingByteArray = instEncoding.GetBytes(this.Album);
			Array.Copy(workingByteArray, 0, tagByteArray, 63, workingByteArray.Length);
			// Copy Year to Array
			workingByteArray = instEncoding.GetBytes(this.Year);
			Array.Copy(workingByteArray, 0, tagByteArray, 93, workingByteArray.Length);
			// Copy Comment to Array
			workingByteArray = instEncoding.GetBytes(this.Comment);
			Array.Copy(workingByteArray, 0, tagByteArray, 97, workingByteArray.Length);
			// Copy Track and Genre to Array
			tagByteArray[126] = System.Convert.ToByte(this.Track);
			tagByteArray[127] = System.Convert.ToByte(this.GenreID);
  
			// SAVE TO DISK: Replace the final 128 Bytes with our new ID3 tag
			FileStream oFileStream = new FileStream(this.filename , FileMode.Open);
			if (this.hasTag)
				oFileStream.Seek(-128, SeekOrigin.End);
			else
				oFileStream.Seek(0, SeekOrigin.End);
			oFileStream.Write(tagByteArray,0, 128);
			oFileStream.Close();
			this.hasTag  = true;
		}
  
	}
}
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
Zeeshan Quireshi is offline  
Old 16-06-2006, 04:07 PM   #3 (permalink)
C# Be Sharp !
 
Zeeshan Quireshi's Avatar
 
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
Default Re: C#.net how to access properties of a file??

Mods , I'm Posting Again Coz All Da Things Will Get Messed Up In One Post !

Also Try This To read Anything Alse Than ID3 Tags :

Code:
Introduction

This article shows, how one omitted the detail information of a file under the use of the Shell32.dll from the Windows system. Some detailed information are: Comment, Title, Subject, Category, Author, and so on.
Background

The Windows system could store additional information like author, comment, title, category... to a file, this information can be read with GetDetailsOf method from the shell32.dll.
Using the code

First of all, add a new COM reference "Microsoft Shell Controls and Automation" (shell32.dll) to your Project.

After adding the reference, simple create an instance of the CFileInfo class as in the demo project. To access the File Information, simply read the properties from the class.

try{
  // Read File Details from CFileInfo Object
  CFileInfo oDetailedFileInfo = new CFileInfo(sFileName);
  txtName.Text = oDetailedFileInfo.FileName;
  txtFiletype.Text = oDetailedFileInfo.FileType;
  txtFileSize.Text = oDetailedFileInfo.FileSize.ToString();
  txtAuthor.Text = oDetailedFileInfo.FileAuthor;
  txtCategory.Text = oDetailedFileInfo.FileCategory;
  txtComment.Text = oDetailedFileInfo.FileComment;
  txtSubject.Text = oDetailedFileInfo.FileSubject;
  txtTitle.Text = oDetailedFileInfo.FileTitle;
}catch(Exception ex){
  MessageBox.Show("Could not read File information\r\n" 
                  + ex.Message, "Error while getting Info", 
                  MessageBoxButtons.OK,MessageBoxIcon.Error);
}

Behind the Scenes

The GetDetailedFileInfo method gets all available information of a file into an ArrayList. The DetailedFileInfo class is a helper class for holding the information in a simple object within the ArrayList.

// Getting all the available Information of a File into a Arraylist
private ArrayList GetDetailedFileInfo(string sFile){
  ArrayList aReturn = new ArrayList();
  if(sFile.Length>0){
    try{
      // Creating a ShellClass Object from the Shell32
      ShellClass sh = new ShellClass();
      // Creating a Folder Object from Folder that inculdes the File
      Folder dir = sh.NameSpace( Path.GetDirectoryName( sFile ) );
      // Creating a new FolderItem from Folder that includes the File
      FolderItem item = dir.ParseName( Path.GetFileName( sFile ) );
      // loop throw the Folder Items
      for( int i = 0; i < 30; i++ ) {
        // read the current detail Info from the FolderItem Object
        //(Retrieves details about an item in a folder. 
        //For example, its size, type, or the time 
        //of its last modification.)

        // some examples:
        // 0 Retrieves the name of the item. 
        // 1 Retrieves the size of the item.
        // 2 Retrieves the type of the item.
        // 3 Retrieves the date and time that the item was last modified.
        // 4 Retrieves the attributes of the item.
        // -1 Retrieves the info tip information for the item. 

        string det = dir.GetDetailsOf( item, i );
        // Create a helper Object for holding the current Information
        // an put it into a ArrayList
        DetailedFileInfo oFileInfo = new DetailedFileInfo(i,det);
        aReturn.Add(oFileInfo);
      }

    }catch(Exception){

    }
  }
  return aReturn;
}
...

// Helper Class from holding the detailed File Informations
// of the System
public class DetailedFileInfo{
  int iID = 0;
  string sValue ="";

  public int ID{
    get{return iID;}
    set{iID=value;}
  }
  public string Value{
    get{return sValue;}
    set{sValue = value;}
  }

  public DetailedFileInfo(int ID, string Value){
    iID = ID;
    sValue = Value;
  }
}
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
Zeeshan Quireshi is offline  
Old 16-06-2006, 05:18 PM   #4 (permalink)
The Frozen Nova
 
casanova's Avatar
 
Join Date: Sep 2004
Location: Trespasser in Virtual Land
Posts: 1,641
Default Re: C#.net how to access properties of a file??

Yes Zeeshan, u figured it right. Ur second post is what I need. But still I am not done. Can u post the complete details of the second post.
__________________
I dream of a better tomorrow... where chickens can cross roads and not have their motives questioned.

www.nerdweed.blogspot.com
casanova is offline  
Old 17-06-2006, 12:34 PM   #5 (permalink)
C# Be Sharp !
 
Zeeshan Quireshi's Avatar
 
Join Date: Jun 2006
Location: Toronto
Posts: 1,805
Default Re: C#.net how to access properties of a file??

Well Here's the zip that you need to get on with the project , it has the complete source code and example !
Attached Files
File Type: zip DetailFileInfo.zip (33.5 KB, 284 views)
__________________
There are 10 types of people in the world: those who understand binary and those who do not.
Zeeshan Quireshi is offline  
Old 09-07-2008, 08:31 PM   #6 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2008
Posts: 1
Default Re: C#.net how to access properties of a file??

Hi,
Looks like the file is corrupted.
can you post the example (download) again.

Thx
rightguy4u is offline  
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


LinkBacks (?)
LinkBack to this Thread: http://www.thinkdigit.com/forum/qna-read-only/30292-c-net-how-access-properties-file.html
Posted By For Type Date
Programming « Stephen’s Personal Blog This thread Refback 12-07-2011 02:47 AM
Read Metada information This thread Refback 27-01-2011 12:05 AM
Anthony Cracked the NTFS File Metadata Problem Stephen’s Personal Blog This thread Refback 26-01-2011 01:51 PM
Read Metada information - www.ms-news.net This thread Refback 17-11-2010 12:22 AM
Read Metada information - VB.NET This thread Refback 21-06-2010 07:09 PM


All times are GMT +5.5. The time now is 09:49 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2