【问题标题】:Could not find a part of the path.Copy a file from an application. WPF找不到路径的一部分。从应用程序复制文件。 WPF
【发布时间】:2014-12-04 02:40:30
【问题描述】:

我的 WPF 应用程序中有一个文件夹 - Helpers/1.png。此文件夹包含图像和 xml 文件。当我从 Visual Studio 运行我的程序时,它工作正常。但是,当我从另一个逻辑磁盘运行程序的“exe”文件时,我看到了这样的错误:

Could not find a part of the path ""..//..//Helpers//1.png"

我希望从运行应用程序的可执行应用程序中复制图像(“1.png”)和 xml 文件,并由用户复制。

如何解决这个错误?

【问题讨论】:

    标签: c# xml wpf image copy


    【解决方案1】:

    您可以通过文件的属性指定项目中的文件会发生什么:

    Build ActionResourceCopy To Output DirectoryCopy always 将意味着该文件将作为文件复制到解决方案的输出目录(即您可以在资源管理器中看到它)。

    如果您在应用程序中使用该文件,它必须存在于磁盘上指定的绝对或相对路径中。否则你会得到你描述的错误。如果我理解正确,您想创建一个自包含的可执行文件,无论它被复制到哪里,它都会运行。这意味着您的应用程序不能依赖任何外部文件。

    要解决这个问题,您需要将所有外部文件嵌入到可执行文件中,并更改您的代码以使用这些嵌入文件,而不是期望磁盘上的文件。

    以下是一种帮助您入门的方法:

    public static byte[] GetResourceAsByteArray(string filename)
        {
            var assembly = Assembly.GetCallingAssembly();
            using (var resFilestream = assembly.GetManifestResourceStream(filename))
            {
                if (resFilestream == null) return null;
                var ba = new byte[resFilestream.Length];
                resFilestream.Read(ba, 0, ba.Length);
                return ba;
            }
        }
    

    要使用它,您需要将文件的构建操作设置为Embedded Resource,并使用文件的完全限定名称调用方法,其中名称组合如下:

    [RootNameSpaceOfTheProject].[NameOfFolderInTheProject].[FileNameWithExtension]

    例子:

    调用方法:

    var b = ResourceOperations.GetResourceAsByteArray("Store.Resources.EmbeddedIcons.toolbox.png");
    

    现在您可以将字节数组写入临时文件并将其用作图像源,或者您可以直接从字节数组构建图像。至少,你有你的数据......

    【讨论】:

      【解决方案2】:

      从另一个逻辑磁盘运行时,文件夹结构是什么样的?你确定文件存在吗?它们实际上是否位于可执行文件位置的下方两个文件夹?

      例如,如果这是您的可执行文件:

      x:\path\to\your\executable.exe
      

      1.png 真的在这里吗? :

      x:\path\Helpers\1.png
      

      如果文件不存在,请检查解决方案中 *.png 文件的属性,以确保每个文件的构建操作都设置为在构建期间将文件复制到输出。

      更新:

      您不能使用“....\”路径来指定 1.png 的位置。您正在做的是告诉可执行文件使用 VS 项目中的 1.png 。构建时,程序会输出到相对路径 bin($Configuration)(bin\Debug 或 bin\Release)。因此,当您执行调试会话时,您的应用程序将进入 VS 项目文件的两个目录级别并获取 1.png。但是,VS 项目不存在于您部署的应用程序中。

      你需要做两件事:

      1) Change your program code to load "Helpers\1.png" instead of "..\..\Helpers\1.png"
      
      2) Highlight 1.png in VS Solution Explorer, right click, and select Properties.  In the    Properties pane change 'Build Action' to 'Copy Always' or 'Copy if Newer'.  (Another response here provided an excellent guide with screenshots)
      

      这样,您的构建过程将创建相对路径“Helpers”并将 1.png 复制到其中。从而保证无论你在哪里部署你的应用,路径和文件都会存在。

      为了演示差异,在进行这些更改之前,导航到包含 VS 项目的文件夹,然后转到路径 bin\Debug。你会看到 Helpers\1.png 在这里不存在。进行我上面概述的两个更改,重建,然后再看看 bin\Debug。您现在将看到 bin\Debug\Helpers\1.png 存在。

      【讨论】:

        【解决方案3】:

        右键单击文件或图像任何东西,然后单击属性。 然后选择Build Action Content,Copy to output Directory As Copy Always。

        如果您正在使用 Install Sheild 构建安装程序

        在添加项目输出文件时选中内容文件复选框。

        您需要根据设置进行编码。

        首先你要声明三个字符串。

            private string strCurrentFolder = "\\give the Project path\\bin\\Debug";
            private string strXMLFolder = "\\give the project folder path\\give folder name where the files stored";
            but the folder sholud be there in Project.
        
            private string strXMLSetupFolder = "\\give folder name where the files stored";
            string XMLFilepath;
        
             if (Application.StartupPath.Contains("Project Name"))
                   XMLFilePath = Application.StartupPath.Replace(strCurrentFolder, strXMLFolder);
            --This is for Visual Studio run
        
            else
                  XMLFilePath = Application.StartupPath + strXMLSetupFolder;
        
            --this is for insatllation folder path
        

        最后你会从 XMLfilepath 字符串中得到文件夹路径。

        XMLFilepath\给出文件名;

        【讨论】:

          【解决方案4】:

          当您在 VS 中调试程序时,它使用不同的“工作目录”运行。这意味着 GetCurrentDirectory 可能不会返回程序实际所在的目录。 按照其他答案中的说明设置构建操作,然后使用此代码获取文件的路径:

          string epath = Assembly.GetExecutingAssembly().Location; // this is the full path to the program
          string filepath = epath.Substring(0, epath.LastIndexOf('\\') + 1) + "Helpers\\1.png"; // this is the full path to the file you need
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-07-23
            • 2018-01-23
            • 2013-01-13
            • 1970-01-01
            相关资源
            最近更新 更多