如何从解决方案资源管理器获取imagefolder的路径 [英] How to get the path of the imagefolder from the solution explorer

查看:60
本文介绍了如何从解决方案资源管理器获取imagefolder的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了一个示例Windows应用程序,当我单击上一个"按钮或下一个"按钮时,将在PictureBox中以幻灯片形式显示图像.
我在项目中创建了一个Image文件夹,其中包含少量图像.

实际上,代码工作正常,但是由于〜/Images无法正常工作,因此我拖放了Image文件夹的路径.它可以在我的系统中正常工作,但由于它包含我的系统路径,因此无法在其他系统中工作.我已经尝试了许多其他方式,但是没有成功.
怎么解决呢?我希望文件夹位于项目本身中.或者我们可以使用资源文件以及如何执行此操作.我不想使用任何openfiledialog.

请检查以下代码:

I have done a sample windows application which will show the images as a slide show in the PictureBox when I click on either Previous button or Next button.
I have created a Image folder in the Project, which contains few images.

Actually the code is working fine, but the problem as ~/Images is not working, so I have dragged and droped the path of the Image folder. It will work fine in my system but it won''t work in other system as it contains my systems path. I have tried in many other ways but, it didnt work.
How to solve this? I want the folder to be in the project itself. Or can we do this using Resource file and how to do it. I dont want to use any openfiledialog.

Please check the below code:

public partial class Form1 : Form
    {
        ArrayList alist = new ArrayList();
        int i = 0;
        int filecount = 0;

        public Form1()
        {
            InitializeComponent();
        }
        
        private void Form1_Load(object sender, EventArgs e)
        {
            //Specify Source folder path here
            System.IO.DirectoryInfo inputDir = new System.IO.DirectoryInfo(@"C:\Users\dss\Desktop\Assignments\Assignments\WinForms Assignments and Documentation\PictureBoxSampleWinApp\PictureBoxSampleWinApp\Images");
            try
            {
                if ((inputDir.Exists))
                {

                    //Get Each jpg files from a source folder and stored in the arraylist
                    System.IO.FileInfo file = null;
                    foreach (System.IO.FileInfo eachfile in inputDir.GetFiles())
                    {
                        file = eachfile;
                        if (file.Extension.ToLower() == ".jpg")
                        {
                            alist.Add(file.FullName);
                            //Store file count here 
                            filecount = filecount + 1;
                        }
                    }
                    //Initally show first image in the picture box
                    pbPictures.Image = Image.FromFile(alist[0].ToString()); 
                    i = 0;
                    btnPrevious.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            //check previous postion image is there or not
            if (i - 1 >= 0)
            {
                pbPictures.Image = Image.FromFile(alist[i - 1].ToString());
                i = i - 1;
                btnNext.Enabled = true;
            }
            else
            {
                btnPrevious.Enabled = false;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            //check next postion image is there or not
            if (i + 1 < filecount)
            {
                pbPictures.Image = Image.FromFile(alist[i + 1].ToString());
                i = i + 1;
                btnPrevious.Enabled = true;
            }
            else
            {
                btnNext.Enabled = false;
            }
        }
    }




谢谢.




Thanks.

推荐答案

您可以使用Application.ExecutablePath.
这将返回启动应用程序的可执行文件的路径和可执行文件名称.

将您的.exe和图片文件夹放在同一文件夹中.

strinf imagepath = Application.ExecutablePath;
imagepath = imagepath.Remove(imagepath.LastIndexOf(''\\'')+1);

imagepath + =您的图像文件夹.
You can use the Application.ExecutablePath.
which will return The path and executable name for the executable file that started the application.

Put your .exe and image folder in same folder.

strinf imagepath= Application.ExecutablePath;
imagepath=imagepath.Remove(imagepath.LastIndexOf(''\\'') + 1);

imagepath+=your image folder.


您需要使用app.config文件存储图像文件夹路径.这样,所有最终用户都可以根据需要配置此路径.
http://msdn.microsoft.com/en-us/library/ms184658.aspx [ ^ ]
You need to use the app.config file to store the image folder path. That way all end users can configure this path as needed.
http://msdn.microsoft.com/en-us/library/ms184658.aspx[^]


在web.config文件中输入以下代码->
-----------------------------------------
put the following code in web.config file->
-----------------------------------------
<configuration>
  <appsettings>
      <add key="imageAddress" value="../HowToOpenPDF/Styles/Site.css" />
      </appsettings>
</configuration>


并在您要使用名称->
访问图像的位置使用以下代码 -------------------------------------------------- ------------------------
ConfigurationManager.AppSettings ["imageAddress"].ToString();

或在按钮上使用javaScript单击:
-------------------------------------
< asp:button runat =服务器" id ="btnConfig"文本=从Web配置读取" xmlns:asp =#unknown">
OnClientClick ="ReadFromWebConfig()"/>


函数ReadFromWebConfig(){
var hk ="& lt;%= ConfigurationManager.AppSettings ["imageAddress"].ToString()%& gt;''
alert(hk);


and use the following code where you want to access the image with name->
--------------------------------------------------------------------------
ConfigurationManager.AppSettings["imageAddress"].ToString();

or using javaScript on button click:
-------------------------------------
<asp:button runat="server" id="btnConfig" text="Read From Web Config" xmlns:asp="#unknown">
OnClientClick="ReadFromWebConfig()"/>


function ReadFromWebConfig() {
var hk = ''&lt;%=ConfigurationManager.AppSettings["imageAddress"].ToString() %&gt;''
alert(hk);


这篇关于如何从解决方案资源管理器获取imagefolder的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆