1

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; } } } 
4
  • I have tried your code. but, it works perfectly. Can you show Form1 code? Commented Mar 3, 2014 at 3:39
  • use MessageBox.Show(Exception.Message); instead of just "ERR". Commented Mar 3, 2014 at 4:00
  • @jade just did the err just to make it pop up anything, to me it pops up nothing, no messageboxes at all. Commented Mar 3, 2014 at 5:16
  • @StevenVenham- i advise u to put only those information in which u are facing problem.. Dont add whole code.. That will become too bulky and people feel bore to read whole content Commented Mar 3, 2014 at 5:26

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the reply, i fixed that, but before i attempted to just place the Picturebox1.Image = Image.fromFile(pram); in the constructor, and it still failed
Did you have tried my test project. it is working perfectly in my computer and i have tested it in windows xp and windows 7. Both constructors works properly. If still it didn't work in you pc then may be the problem is in your os. Place a message box in the Program.cs -> Main function. MessageBox.Show(argument);
Interesting, after selecting yours as the one to run on clicking, it worked, then as i selected it back to mine it all of a sudden started working. That is some crazy stuff.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.