launch an OS associated application from C#

by sondlerd
AddThis Social Bookmark Button

12. September 2007 21:18

 

 Here is a snippet of code that will allow you to invoke the OS to launch the application associated with the OS file mapping.

this is C# 3.0 but mostly C#2.0 DLLs

using System.IO.FileInfo;
using System.IO.FileStream;

try

{

Diagram diagram = (Diagram)ListView.SelectedItem;

string tempFile = System.IO.Path.Combine(System.IO.Path.GetTempPath(), System.IO.Path.GetRandomFileName());

FileInfo fileInfo = new FileInfo(diagram.Filename);

tempFile += fileInfo.Extension;

FileStream fs = System.IO.File.Create(tempFile);

fs.Write(diagram.Diagram, 0, diagram.Diagram.Length); // diagram.Diagram here is a byte[] of the actually diagram object

fs.Close();

System.Diagnostics.
Process.Start(tempFile);

}

catch (System.ComponentModel.Win32Exception e)

{

//No application is associated with this file.

WorkbenchApplication.ShowMessageToUser("There is no application associated with this file extension.");

}

 

I did not write this code, a co worker, Marrio Matriccino found it and passed it onto me.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

General | C# 2.0 | C# | FileInfo | FileStream | File IO

Related posts

Add comment


(Will show your Gravatar icon)  

  Country flag




Live preview

November 21. 2008 22:26

Gravatar

Powered by BlogEngine.NET 1.1.0.7
Theme by Mads Kristensen

Robert D. Sondles

Name of author Robert Sondles
is an independent software engineer operating under his company named Blueberry Island Media ltd. He started the company in 2003 and has been developing softare with .Net since 2002. He is located in Philadelphia, PA..

E-mail me Send mail

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in