Ok so i wrote my own photo viewer to open jpg,gif,png files on my computer. However for some reason whenever i set the file association in windows, using the normal properties menu and then selecting my exe it fails to open the program when i click a picture.

I tried debugging by adding message boxes, but sofar it gives no output.

I see the current window loose focus, but nothing appears. And task manager does not show my process ever opening.

I think windows might be preventing my application from running in some way, iv attempted to disable my antivirus and running it thinking it was that, but no dice.

Program.cs

namespace PictureViewer
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            if (args == null || args.Length == 0)
            {
                //Console.WriteLine("args is null"); // Check for null array
                Application.Run(new Form1());
            }
            else
            {
                for (int i = 0; i < args.Length; i++) // Loop through array
                {
                    string argument = args[i];
                    Application.Run(new Form1(argument));
                }
            }
        }
    }
}

Inside Form1 is 2 constructors, 1 with and one without a pram of string then i just do a

Picturebox1.Image = Image.fromFile(pram);

Im quite sure this issent a c# thing, its more of a windows being dumb thing. Windows 8.1 for refrence.

edit: heres form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PictureViewer
{
    public partial class Form1 : Form
    {
        string curentdirectory = "";
        List<string> imageindir;
        int curentindex;
        public Form1()
        {
            InitializeComponent();
            imageindir = new List<string>();
        }
        public Form1(string initfile)
        {
            InitializeComponent();
            curentdirectory = initfile.Substring(0, initfile.LastIndexOf("/"));
            imageindir = new List<string>();
            try
            {
                this.Text = initfile;
                img.Image = Image.FromFile(initfile);
            }
            catch(Exception ex)
            {
                MessageBox.Show("ERR");
            }

        }

        private void btnleft_Click(object sender, EventArgs e)
        {
            try
            {
                if (--curentindex < 0)
                {
                    curentindex = imageindir.Count - 1;
                }
                img.Image = Image.FromFile(imageindir[curentindex]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERR");
            }

        }

        private void btnright_Click(object sender, EventArgs e)
        {
            try
            {
                if (++curentindex > imageindir.Count - 1)
                {
                    curentindex = 0;
                }
                img.Image = Image.FromFile(imageindir[curentindex]);
            }
            catch (Exception ex)
            {
                MessageBox.Show("ERR");
            }

        }

        private void getDirFromFileName(string dir)
        {
            DirectoryInfo di;

            di = new DirectoryInfo(curentdirectory);
            var directories = di.GetFiles("*", SearchOption.TopDirectoryOnly);
            foreach (FileInfo d in directories)
            {
                if(dir == d.Name)
                {
                    curentindex = imageindir.Count;
                }
                if(validExtension(d.Name))
                {
                    imageindir.Add(d.Name);
                }
            }
        }

        private bool validExtension(string val)
        {
            val = val.ToLower();
            if (val.Contains(".jpg") || val.Contains(".jpeg") || val.Contains(".gif") || val.Contains(".png") || val.Contains(".bmp"))
                return true;
            return false;
        }
    }
}
有帮助吗?

解决方案

There is an error in curentdirectory = initfile.Substring(0, initfile.LastIndexOf("/")); line. the / should be \\. May be the problem is here. I have tested your code and it works fine. i have uploaded test project here

Editional Details: Project has created in Visual Studio 2005.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top