forum

Detect the osu! folder easily (C# / VB.NET / Java)

posted
Total Posts
5
Topic Starter
Flanster
For those who are working with IO related tasks in their applications for osu!

Instead of guessing and letting the user find their osu! path with that annoying little folder browser, it'll pick it up for you.
Doesn't matter if osu! was installed or is just in portable mode.

Some of the code was made in a console app but it can be used in winform apps.
Examples are available in the current programming languages:

C# Method 1 - via registry
C# Method 2 - via running process
VB.NET
Java

euphyy_old
Good to know. xD

If anyone needs this in Java, here's some JNA code and a terrible regex:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.sun.jna.platform.win32.Advapi32Util;
import com.sun.jna.platform.win32.Win32Exception;
import com.sun.jna.platform.win32.WinReg;

String getOsuInstallationDirectory() {
if (!System.getProperty("os.name").startsWith("Win")) return null;
String value;
try {
value = Advapi32Util.registryGetStringValue(WinReg.HKEY_CLASSES_ROOT, "osu\\DefaultIcon", null);
} catch (Win32Exception e) { return null; }
Matcher m = Pattern.compile("\"(.+)\\\\[^\\/]+\\.exe\"").matcher(value);
return (m.find()) ? m.group(1) : null;
}
Piotrekol
Or another way - get it from currently running osu! instance.
C#
private string GetOsuRunningDirectory()
{
var processes = Process.GetProcessesByName("osu!");
if (processes.Length > 0)
{
string dir = processes[0].Modules[0].FileName;
dir = dir.Remove(dir.LastIndexOf('\\'));
return dir;
}
return string.Empty;
}
Topic Starter
Flanster
Thanks for the additions, guys!
Sieg
With custom directory support, kudos to TicClick

String GetSongsDirectory()
{
const String keyName1 = @"HKEY_CLASSES_ROOT\osu\shell\open\command";
const String keyName2 = @"HKEY_CLASSES_ROOT\osu!\shell\open\command";
String path = String.Empty, songsPath = String.Empty;
try
{
path = Microsoft.Win32.Registry.GetValue(keyName1, String.Empty, String.Empty).ToString();
if (path == String.Empty)
path = Microsoft.Win32.Registry.GetValue(keyName2, String.Empty, String.Empty).ToString();
if (path != String.Empty)
{
path = path.Remove(0, 1);
path = path.Split('\"')[0];
path = System.IO.Path.GetDirectoryName(path);

String iniPath = System.IO.Path.Combine(path, "osu!." + Environment.UserName + ".cfg");
StreamReader s = File.OpenText(iniPath);

while (!s.EndOfStream && !(songsPath != String.Empty))
{
string l = s.ReadLine();
if (l.StartsWith("#"))
continue;

string[] separator = { " = " };
string[] kv = l.Split(separator, StringSplitOptions.None);
if (kv[0] == "BeatmapDirectory")
songsPath = kv[1];
}

if(songsPath == string.Empty)
songsPath = System.IO.Path.Combine(path, "Songs");
}
}
catch
{
//probaly security or io exception or unexpected value from registry
//Beatmap directory undetected
return "";
}
if(System.IO.Path.IsPathRooted(songsPath))
return songsPath;
else
return System.IO.Path.Combine(path,songsPath);
}
Please sign in to reply.

New reply