Jump to content


Photo

C# help


  • Please log in to reply
2 replies to this topic

#1 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 16 November 2009 - 11:23 PM

I suck at C#.

I am trying to retrieve a directory from the registry using C#. The key (in my instance) returns:

"\"c:\\program files (x86)\\steam\\steamapps\\common\\left 4 dead 2 demo\\left4dead2.exe\" \"%1\""

Question is, how do I turn that into just the usable directory ("...\left 4 dead 2 demo\") so I can try to tie it into other strings ("...\left 4 dead 2 demo\steam_appid.txt", etc.)

// Retrieve L4D2 directory
string L4D2Dir = Registry.GetValue("HKEY_CLASSES_ROOT\\Applications\\left4dead2.exe\\shell\\open\\command", "", "\"c:\\program files\\steam\\steamapps\\common\\left 4 dead 2 demo\\left4dead2.exe\" \"%1\"").ToString();

Also; won't won't this work? I'm trying to write "If Steam.exe is not running, do thus"
Process[] pname = Process.GetProcessesByName("Steam.exe");
if (pname.Length == 0)
{
//code
}

World Domination Status: 2.7%


#2 Bart

Bart

  • Network Admins
  • 8,524 posts
  • Location:The Netherlands
  • Division:Revora
  • Job:Network Leader

Posted 16 November 2009 - 11:53 PM

Hmm, why would you get the result with escaping slashes? That's weird. Is that how it's actually in the registry or does .NET add that?
Anyway, you have to find some class they use to represent a files and directories (called File or Path or whatever). That one will probably provide reliable methods to dissect the path and get what you need.
bartvh | Join me, make your signature small!
Einstein: "We can’t solve problems by using the same kind of thinking we used when we created them."

#3 Jeeves

Jeeves

    I write the interwebz

  • Members
  • 4,156 posts
  •  Friendly neighborhood standards Nazi

Posted 17 November 2009 - 01:23 AM

Thanks 2play ^.^
Got it working with the following:
// Retrieve L4D2 directory
string RegDir = Registry.GetValue("HKEY_CLASSES_ROOT\\Applications\\left4dead2.exe\\shell\\open\\command", "", "\"c:\\program files\\steam\\steamapps\\common\\left 4 dead 2 demo\\left4dead2.exe\" \"%1\"").ToString();
int found = RegDir.IndexOf("left4dead2.exe");
RegDir = RegDir.Substring(1).Remove(found);
RegDir = RegDir;

string L4D2Dir = Path.GetDirectoryName(RegDir);
So cut off the opening "\ and ending the string where left4dead2.exe starts, you're left with a directory path.

World Domination Status: 2.7%





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users