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
Solved

Translate 6 lines of code to C#?

Posted on 2011-02-23
8
1,075 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
  • 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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