Solved

Translate 6 lines of code to C#?

Posted on 2011-02-23
8
1,078 Views
Last Modified: 2012-05-11
I'm good at programming AutoHotkey scripts. I'm learning C#. Could someone translate this snippet of code to C# for me?

  objShell := ComObjCreate("Shell.Application")
  objFolder := objShell.Namespace(TargetFolder . "\")

  i := 0
  loop, 30
  {
    Tstr := objFolder.GetDetailsOf(objFolder.Items, i)
    <write Tstr to console>
  }

Open in new window

0
Comment
Question by:deleyd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959742
Not being familiar with AutoHotKeys myself, maybe you could explain what the code is doing?
0
 
LVL 1

Expert Comment

by:DOTNET_GURU
ID: 34959824
 objShell = new ComObjCreate("Shell.Application");
  objFolder = objShell.Namespace("TargetFolder");

  for (int i=0;i<30;i++)
  {
    Tstr = objFolder.GetDetailsOf(objFolder.Items, i);
    Console.WriteLine("You Output : " + Tstr);
  }

0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34959841
Yeah, I think it's going to take a little more than just changing it to look a little more like C# syntax!!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:mac-will
ID: 34964422
You are printing the contents of a directory to the console?

[NOTE: using System.IO;]


            DirectoryInfo source = new DirectoryInfo(@"\");
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                Console.WriteLine("Directory: " + diSourceSubDir.Name);
            }

            foreach (FileInfo fi in source.GetFiles())
            {
                Console.WriteLine("File: " + fi.Name);
            }

Open in new window

0
 

Author Comment

by:deleyd
ID: 34966299
OK I see I need to give a bit more of what I'm trying to do. In Windows File Explorer it lists various attributes for a file. Under VIEW -> CHOOSE DETAILS there's an extensive list of things I can list about a file, such as if it's a music file I can list the 'Album', 'Artists', 'Title', 'Comments', 'Year', etc.

I can get this info from Shell32 (C:\Windows\System32\shell32.dll). The AutoHotkey script above does this using a COM interface.

I bet there's a better way in C# since COM is sort of old and going obsolete from what I've been reading lately.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 250 total points
ID: 34967799
Actually I think you'll still need to use Shell. So you'll need to add a reference to the "Microsoft Shell Controls And Automation" from the COM tab of the Add Reference dialog.

After that, you can try:
            Shell32.Shell shell = new Shell32.Shell();
            Shell32.Folder folder = shell.NameSpace(@"somefolder");

            for (int i = 0; i != 30; ++i)
            {
                string value = folder.GetDetailsOf(folder, i);
                Console.WriteLine(value);
            }

Open in new window

0
 
LVL 8

Assisted Solution

by:mac-will
mac-will earned 250 total points
ID: 34969988
good find @carl_tawn!

Extending this a little:

            DirectoryInfo source = new DirectoryInfo(@"C:\Users\NAME\Music");

            Shell32.Shell shell = new Shell32.Shell();
            Shell32.Folder folder = shell.NameSpace(source.FullName);

            foreach (FileInfo fi in source.GetFiles())
            {
                Shell32.FolderItem fileItem = folder.ParseName(fi.Name);
                for (int i = 0; i != 50; ++i)
                {
                    string attrib = folder.GetDetailsOf(folder, i);
                    string value = folder.GetDetailsOf(fileItem, i);
                    Console.WriteLine(attrib + " : " + value);
                }
            }

Open in new window

0
 

Author Closing Comment

by:deleyd
ID: 34990416
Super! (thought I already said "Super!". Surprised to see question is still open. OK I'll say "Super!" again. See if it works this time.)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question