Das Finden der Standardanwendung für einen bestimmten Dateityp auf Windows öffnen
Frage
Ich entwickle eine Anwendung Targeting .NET Framework 2.0 mit C #, für die ich brauche, um die Standardanwendung zu finden in der Lage sein, die zum Öffnen eines bestimmten Dateityp verwendet wird.
Ich weiß, dass, zum Beispiel, wenn Sie nur eine Datei zu öffnen, indem die Anwendung, die Sie so etwas wie verwenden können:
System.Diagnostics.Process.Start( "C:\...\...\myfile.html" );
ein HTML-Dokument im Standard-Browser zu öffnen, oder
System.Diagnostics.Process.Start( "C:\...\...\myfile.txt" );
eine Textdatei im Standard-Texteditor zu öffnen.
Doch was ich will, ist zu tun, um der Lage sein, Dateien zu öffnen, die eine nicht notwendigerweise .txt Erweiterung (zum Beispiel), im Standard-Texteditor, so muss ich sein der Lage, die Standardanwendung zum Öffnen, um herauszufinden, .txt Dateien, die mir erlauben, es direkt aufzurufen.
Ich vermute, es gibt einig Win32-API, die ich P / Invoke brauchen werden, um dies zu tun, aber ein kurzer Blick mit Google und MSDN hat nichts von großen Interesse zeigen; Ich habe eine sehr große Anzahl von völlig irrelevanten Seiten, aber nichts, wie ich suche.
Lösung
Sie können für die Erweiterung und Aktions Details Registrierungsabschnitt HKEY_CLASSES_ROOT
überprüfen unter. Dokumentation hierfür ist auf MSDN . Alternativ können Sie verwenden, um den IQueryAssociations Schnittstelle.
Andere Tipps
Alle aktuellen Antworten sind unzuverlässig. Die Registrierung ist ein Detail Implementierung und in der Tat ein solcher Code ist auf meinem Windows 8.1 Maschine gebrochen. Der richtige Weg, dies zu tun, wird unter Verwendung des Win32-API, speziell AssocQueryString :
using System.Runtime.InteropServices;
[DllImport("Shlwapi.dll", CharSet = CharSet.Unicode)]
public static extern uint AssocQueryString(
AssocF flags,
AssocStr str,
string pszAssoc,
string pszExtra,
[Out] StringBuilder pszOut,
ref uint pcchOut
);
[Flags]
public enum AssocF
{
None = 0,
Init_NoRemapCLSID = 0x1,
Init_ByExeName = 0x2,
Open_ByExeName = 0x2,
Init_DefaultToStar = 0x4,
Init_DefaultToFolder = 0x8,
NoUserSettings = 0x10,
NoTruncate = 0x20,
Verify = 0x40,
RemapRunDll = 0x80,
NoFixUps = 0x100,
IgnoreBaseClass = 0x200,
Init_IgnoreUnknown = 0x400,
Init_Fixed_ProgId = 0x800,
Is_Protocol = 0x1000,
Init_For_File = 0x2000
}
public enum AssocStr
{
Command = 1,
Executable,
FriendlyDocName,
FriendlyAppName,
NoOpen,
ShellNewValue,
DDECommand,
DDEIfExec,
DDEApplication,
DDETopic,
InfoTip,
QuickTip,
TileInfo,
ContentType,
DefaultIcon,
ShellExtension,
DropTarget,
DelegateExecute,
Supported_Uri_Protocols,
ProgID,
AppID,
AppPublisher,
AppIconReference,
Max
}
Relevante Dokumentation:
Proben Nutzung:
static string AssocQueryString(AssocStr association, string extension)
{
const int S_OK = 0;
const int S_FALSE = 1;
uint length = 0;
uint ret = AssocQueryString(AssocF.None, association, extension, null, null, ref length);
if (ret != S_FALSE)
{
throw new InvalidOperationException("Could not determine associated string");
}
var sb = new StringBuilder((int)length); // (length-1) will probably work too as the marshaller adds null termination
ret = AssocQueryString(AssocF.None, association, extension, null, sb, ref length);
if (ret != S_OK)
{
throw new InvalidOperationException("Could not determine associated string");
}
return sb.ToString();
}
Doh! Natürlich.
HKEY_CLASSES_ROOT\.txt
enthält einen Verweis auf
HKEY_CLASSES_ROOT\txtfile
, welche enthält einen Unterschlüssel
HKEY_CLASSES_ROOT\txtfile\shell\open\command
, welche Hinweise Editor.
Sortiert, vielen Dank!
Bart
Hier ist eine Blog-Post mit darüber Thema. Die Codebeispiele in VB.net sind, aber es sollte zu portieren, sie zu C # einfach sein.
Sie können nur die Registrierung abfragen. Zuerst sollten Sie den Standardeintrag unter HKEY_CLASSES_ROOT \ .ext
Das gibt Ihnen die Klassennamen. Zum Beispiel hat .txt eine Vorgabe von txtfile
Dann öffnen HKEY_CLASSES_ROOT \ txtfile \ Shell \ Open \ Command
Das gibt Ihnen den Standardbefehl verwendet wird.
Eine späte Antwort, aber es gibt ein gutes NuGet Paket, das Dateizuordnung behandelt: Dateizuordnung
Die Verwendung ist einfach, zum Beispiel alle erlaubten Dateierweiterungen ein Kontextmenü hinzuzufügen:
private void OnMenuSourceFileOpening(object sender, ...)
{ // open a context menu with the associated files + ".txt" files
if (File.Exists(this.SelectedFileName))
{
string fileExt = Path.GetExtension(this.SelectedFileNames);
string[] allowedExtensions = new string[] { fileExt, ".txt" };
var fileAssociations = allowedExtensions
.Select(ext => new FileAssociationInfo(ext));
var progInfos = fileAssociations
.Select(fileAssoc => new ProgramAssociationInfo (fileAssoc.ProgID));
var toolstripItems = myProgInfos
.Select(proginfo => new ToolStripLabel (proginfo.Description) { Tag = proginfo });
// add also the prog info as Tag, for easy access
// when the toolstrip item is selected
// of course this can also be done in one long linq statement
// fill the context menu:
this.contextMenu1.Items.Clear();
this.contextMenuOpenSourceFile.Items.AddRange (toolstripItems.ToArray());
}
}