Solved

Translate 6 lines of code to C#?

Posted on 2011-02-23
8
1,067 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
 
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now