Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法将图像设置为使用我的程序打开,为什么不?_C#_Windows_Winforms_File Association - Fatal编程技术网

C# 无法将图像设置为使用我的程序打开,为什么不?

C# 无法将图像设置为使用我的程序打开,为什么不?,c#,windows,winforms,file-association,C#,Windows,Winforms,File Association,好的,所以我写了我自己的照片查看器在我的电脑上打开jpg,gif,png文件。然而,由于某些原因,每当我在windows中设置文件关联时,使用“普通属性”菜单,然后选择“我的exe”,当我单击图片时,它无法打开程序 我尝试通过添加消息框进行调试,但到目前为止它没有输出 我看到当前窗口没有焦点,但没有显示任何内容。 而且任务管理器不会显示我的流程一直处于打开状态 我认为windows可能会以某种方式阻止我的应用程序运行,iv试图禁用我的防病毒软件,并在运行时认为是这样,但没有任何风险 Progra

好的,所以我写了我自己的照片查看器在我的电脑上打开jpg,gif,png文件。然而,由于某些原因,每当我在windows中设置文件关联时,使用“普通属性”菜单,然后选择“我的exe”,当我单击图片时,它无法打开程序

我尝试通过添加消息框进行调试,但到目前为止它没有输出

我看到当前窗口没有焦点,但没有显示任何内容。 而且任务管理器不会显示我的流程一直处于打开状态

我认为windows可能会以某种方式阻止我的应用程序运行,iv试图禁用我的防病毒软件,并在运行时认为是这样,但没有任何风险

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));
                }
            }
        }
    }
}
我很确定这不是一件愚蠢的事情,更像是一件窗户被愚弄的事情。 Windows 8.1作为参考

编辑:这里是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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.IO;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
命名空间图片浏览器
{
公共部分类Form1:Form
{
字符串curentdirectory=“”;
列表成像仪;
int curentindex;
公共表格1()
{
初始化组件();
imageindir=新列表();
}
公共表单1(字符串初始化文件)
{
初始化组件();
currentdirectory=initfile.Substring(0,initfile.LastIndexOf(“/”);
imageindir=新列表();
尝试
{
this.Text=initfile;
img.Image=Image.FromFile(initfile);
}
捕获(例外情况除外)
{
MessageBox.Show(“ERR”);
}
}
私有无效btnleft\u单击(对象发送方,事件参数e)
{
尝试
{
如果(--curentindex<0)
{
curentindex=成像仪计数-1;
}
img.Image=Image.FromFile(imagindir[curentindex]);
}
捕获(例外情况除外)
{
MessageBox.Show(“ERR”);
}
}
私有无效BTN右键单击(对象发送者,事件参数e)
{
尝试
{
如果(++curentindex>imageindir.Count-1)
{
curentindex=0;
}
img.Image=Image.FromFile(imagindir[curentindex]);
}
捕获(例外情况除外)
{
MessageBox.Show(“ERR”);
}
}
私有void getDirFromFileName(字符串目录)
{
目录信息di;
di=新目录信息(当前目录);
var directories=di.GetFiles(“*”,SearchOption.TopDirectoryOnly);
foreach(目录中的文件信息)
{
if(dir==d.Name)
{
curentindex=成像仪计数;
}
if(有效性(d.名称))
{
imageindir.Add(d.Name);
}
}
}
专用布尔值validExtension(字符串val)
{
val=val.ToLower();
如果(val.Contains(“.jpg”)| | val.Contains(“.jpeg”)| | val.Contains(“.gif”)| | val.Contains(“.png”)| | val.Contains(.bmp”))
返回true;
返回false;
}
}
}

curentdirectory=initfile.Substring(0,initfile.LastIndexOf(“/”)中有错误行。
/
应该是
\
。问题可能就在这里。 我已经测试了你的代码,效果很好。我已经上传了测试项目

编辑详细信息
Project已在Visual Studio 2005中创建。

我已尝试了您的代码。但是,它工作得非常好。可以显示Form1代码吗?使用MessageBox.show(Exception.Message);不是“呃”。@jade只是为了让它弹出任何东西,对我来说它什么也不弹出,根本没有消息框。@StevenVenham-我建议你只把你面临问题的信息放进去。。不要添加整个代码。。这会变得太笨重,人们会觉得无聊而无法阅读全部内容。感谢您的回复,我已经解决了这个问题,但在我尝试放置Picturebox1.Image=Image.fromFile(pram)之前;在构造函数中,它仍然失败。您是否尝试过我的测试项目。它在我的电脑上运行得很好,我已经在WindowsXP和Windows7上对它进行了测试。两个构造函数都工作正常。如果它仍然不能在你的电脑上工作,那么问题可能出在你的操作系统上。在Program.cs->Main函数中放置一个消息框<代码>MessageBox.Show(参数)有趣,在点击选择你的作为要运行的一个之后,它工作了,然后当我选择它回到我的位置时,它突然开始工作了。那是一些疯狂的东西。
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;
        }
    }
}