WPF程序设置扩展名类型文件默认打开程序(程序需要以管理员方式启动)
public class Startup { [STAThread] public static void Main(string[] args) { SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper(); wrapper.Run(args); } } public class SingleInstanceApplicationWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase { public SingleInstanceApplicationWrapper() { // Enable single-instance mode. this.IsSingleInstance = true; } // Create the WPF application class. private WpfApp app; protected override bool OnStartup( Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e) { string extension = ".testDoc"; string title = "SingleInstanceApplication"; string extensionDescription = "A Test Document"; // Uncomment this line to create the file registration. // In Windows Vista, you'll need to run the application // as an administrator. FileRegistrationHelper.SetFileAssociation( extension, title + "." + extensionDescription); app = new WpfApp(); app.Run(); return false; } // Direct multiple instances protected override void OnStartupNextInstance( Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e) { if (e.CommandLine.Count > 0) { app.ShowDocument(e.CommandLine[0]); } } } public class WpfApp : System.Windows.Application { protected override void OnStartup(System.Windows.StartupEventArgs e) { base.OnStartup(e); // Load the main window. DocumentList list = new DocumentList(); this.MainWindow = list; list.Show(); // Load the document that was specified as an argument. if (e.Args.Length > 0) ShowDocument(e.Args[0]); } // An ObservableCollection is a List that provides notification // when items are added, deleted, or removed. It's preferred for data binding. private ObservableCollection<DocumentReference> documents = new ObservableCollection<DocumentReference>(); public ObservableCollection<DocumentReference> Documents { get { return documents; } set { documents = value; } } public void ShowDocument(string filename) { try { Document doc = new Document(); DocumentReference docRef = new DocumentReference(doc, filename); doc.LoadFile(docRef); doc.Owner = this.MainWindow; doc.Show(); doc.Activate(); Documents.Add(docRef); } catch { MessageBox.Show("Could not load document."); } } }
通过这种方式,下次默认双击这种类型的文件时你的程序就会被设置为默认启动软件。
专业从事基于C#,WinForm ,WPF,Silverlight,WCF以及MS Sql Server 2000/2005/2008/2012 Oracle 9i/10g/11g数据库系统的ERP,CRM,企业进销存等各种数据库管理系统开发。Asp.net,Asp.net mvc,Webservice,WCF, Webapi等服务程序开发。
基于Oracle MySQL MSSql postgresql各种数据库的管理系统数据同步服务。以及基于MapXtreme, Arcgis Engine ,以及基于Arcgis for silverlight/Javascript的WebGIS等相关的GIS系统二次开发。基于Windows 10 Mobile的移动端开发方案。针对各种系统的二次开发维护,并提供相关开发的技术性支持,如程序BUG解决,应用系统架构,技术难题攻克等相关技术服务。
联系方式: QQ :80163278(devgis) 邮箱:devgis@qq.com