在Visual Studio中发布具有自定义应用程序根文件夹的c#项目

问题描述 投票:0回答:1

我需要一些帮助。我已经完成了对应用程序的编码,可以正常工作,但是发布该应用程序时遇到了一些挑战。我已经设法将数据库复制到输出目录,但是,将imagess目录添加到输出目录对我来说已经成为一个很大的问题。图片目录包含可通过应用上传的自定义图片。我对.csproj做了一些更改,但没有成功。

。csproj

<Target Name="ContentsBeforeBuild" AfterTargets="BeforeBuild">
    <ItemGroup>
      <Content Include="images\*.*">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
    </ItemGroup>
  </Target>

也使用

<Target Name="ContentsBeforeBuild" AfterTargets="BeforeBuild">
    <ItemGroup>
      <Content Include="images\**">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </Content>
    </ItemGroup>
  </Target>

这是用于将图像检索并显示到我的应用程序上的代码。在发布应用程序之前,它运行良好,但是在发布之后,它返回目录未找到异常。

 String paths = Application.StartupPath.Substring(0, (Application.StartupPath.Length - 10));

                    String imagePath = paths+"\\images\\"+col["itemPhoto"].ToString();

错误。发布后无法访问图像目录

The image not found directory error

我是否可以通过其他任何方式编写代码,以便从应用程序获取图像?Kindy我需要一些帮助。

c# visual-studio
1个回答
1
投票

我认为问题在于您尝试访问图像文件夹的方式。就像我在评论中说的那样,在部署应用程序后,路径将不匹配(因为您正在处理子字符串)。

因为您正在复制图像文件夹,所以不需要做子字符串。

只需尝试一下:

String imagePath = Application.StartupPath+"\\images\\"+col["itemPhoto"].ToString();
© www.soinside.com 2019 - 2024. All rights reserved.