C# 文件关联 (jpg等图片格式为例)

 
View Code
//使用方法:右键要打开的图片文件,打开方式-选择默认程序-浏览- pic.exe



//Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;



using System.IO;



namespace pic

{

    public partial class Form1 : Form

    {

        private static string openfilepath = "";



        public Form1()

        {

            InitializeComponent();

        }



        public Form1(String[] args)

        {

            InitializeComponent();

            //打开前会显示文件的路径

            MessageBox.Show(args[0]);

            try

            {

                openfilepath = args[0];

                Image img = Image.FromFile(openfilepath);

                pictureBox1.Image = img;

            }

            catch

            {

                MessageBox.Show("打开文件出错!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);

                return;

            }           

        }

    }

}





//Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Windows.Forms;



namespace pic

{

    static class Program

    {

        /// <summary>

        /// 应用程序的主入口点。

        /// </summary>

        [STAThread]

        static void Main(String[] args)

        {

            Application.EnableVisualStyles();

            Application.SetCompatibleTextRenderingDefault(false);          



            if (args.Length > 0)

                Application.Run(new Form1(args));

            else

                Application.Run(new Form1());

        }

    }

}

 

 

 

你可能感兴趣的:(jpg)