首页 > 解决方案 > 在 Visual Studio 中使用自定义应用程序根文件夹发布 c# 项目

问题描述

我需要一些帮助。我已经完成了我的应用程序的编码工作,但我在发布应用程序时遇到了一些挑战。我已经设法将数据库复制到输出目录但是,将图像目录添加到输出目录对我来说已经成为一个非常大的问题。图像目录包含可通过应用上传的自定义图像。我对 .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();

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

图片未找到目录错误

他们有什么其他方式我可以编写代码以便从应用程序中获取图像吗?我需要一些帮助。

一个多星期试图发布我的应用程序..请帮助

.csproj

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProjectGuid>{5765EB29-19B9-4FC8-8414-B033C956AE48}</ProjectGuid>
    <OutputType>WinExe</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Shop_POS</RootNamespace>
    <AssemblyName>Shop POS</AssemblyName>
    <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
    <FileAlignment>512</FileAlignment>
    <IsWebBootstrapper>false</IsWebBootstrapper>
    <PublishUrl>C:\Users\dansyo\Desktop\pos\spos\</PublishUrl>
    <Install>true</Install>
    <InstallFrom>Disk</InstallFrom>
    <UpdateEnabled>false</UpdateEnabled>
    <UpdateMode>Foreground</UpdateMode>
    <UpdateInterval>7</UpdateInterval>
    <UpdateIntervalUnits>Days</UpdateIntervalUnits>
    <UpdatePeriodically>false</UpdatePeriodically>
    <UpdateRequired>false</UpdateRequired>
    <MapFileExtensions>true</MapFileExtensions>
    <ApplicationRevision>2</ApplicationRevision>
    <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
    <UseApplicationTrust>false</UseApplicationTrust>
    <PublishWizardCompleted>true</PublishWizardCompleted>
    <BootstrapperEnabled>true</BootstrapperEnabled>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <PlatformTarget>AnyCPU</PlatformTarget>
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup>
    <ApplicationIcon>logo1_15W_icon.ico</ApplicationIcon>
  </PropertyGroup>
  <PropertyGroup>
    <ManifestCertificateThumbprint>986913D78C02BC6448BB7EBB687285F9D3AB2FD4</ManifestCertificateThumbprint>
  </PropertyGroup>
  <PropertyGroup>
    <ManifestKeyFile>Shop POS_TemporaryKey.pfx</ManifestKeyFile>
  </PropertyGroup>
  <PropertyGroup>
    <GenerateManifests>true</GenerateManifests>
  </PropertyGroup>
  <PropertyGroup>
    <SignManifests>true</SignManifests>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="System" />
    <Reference Include="System.Core" />
    <Reference Include="System.DirectoryServices" />
    <Reference Include="System.Windows.Forms.DataVisualization" />
    <Reference Include="System.Xml.Linq" />
    <Reference Include="System.Data.DataSetExtensions" />
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="System.Data" />
    <Reference Include="System.Deployment" />
    <Reference Include="System.Drawing" />
    <Reference Include="System.Windows.Forms" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="AddEmployeeDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="AddEmployeeDialog.Designer.cs">
      <DependentUpon>AddEmployeeDialog.cs</DependentUpon>
    </Compile>
    <Compile Include="AddItemDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="AddItemDialog.Designer.cs">
      <DependentUpon>AddItemDialog.cs</DependentUpon>
    </Compile>
    <Compile Include="ComboboxItem.cs" />
    <Compile Include="BusinessProfile.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="BusinessProfile.Designer.cs">
      <DependentUpon>BusinessProfile.cs</DependentUpon>
    </Compile>
    <Compile Include="DatabaseHandler.cs" />
    <Compile Include="DeleteAlldata.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="DeleteAlldata.Designer.cs">
      <DependentUpon>DeleteAlldata.cs</DependentUpon>
    </Compile>
    <Compile Include="ForgottenPassword.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="ForgottenPassword.Designer.cs">
      <DependentUpon>ForgottenPassword.cs</DependentUpon>
    </Compile>
    <Compile Include="GenerateBarCode.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="GenerateBarCode.Designer.cs">
      <DependentUpon>GenerateBarCode.cs</DependentUpon>
    </Compile>
    <Compile Include="GenerateBarCodeDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="GenerateBarCodeDialog.Designer.cs">
      <DependentUpon>GenerateBarCodeDialog.cs</DependentUpon>
    </Compile>
    <Compile Include="OutOfStock.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="OutOfStock.Designer.cs">
      <DependentUpon>OutOfStock.cs</DependentUpon>
    </Compile>
    <Compile Include="PremiumActivation.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="PremiumActivation.Designer.cs">
      <DependentUpon>PremiumActivation.cs</DependentUpon>
    </Compile>
    <Compile Include="RecoverAccount.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="RecoverAccount.Designer.cs">
      <DependentUpon>RecoverAccount.cs</DependentUpon>
    </Compile>
    <Compile Include="Getter.cs" />
    <Compile Include="MainActivity.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="MainActivity.Designer.cs">
      <DependentUpon>MainActivity.cs</DependentUpon>
    </Compile>
    <Compile Include="Login.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Login.Designer.cs">
      <DependentUpon>Login.cs</DependentUpon>
    </Compile>
    <Compile Include="Methods.cs" />
    <Compile Include="PrintMyCart.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="PrintMyCart.Designer.cs">
      <DependentUpon>PrintMyCart.cs</DependentUpon>
    </Compile>
    <Compile Include="Program.cs" />
    <Compile Include="Properties\AssemblyInfo.cs" />
    <Compile Include="Register.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Register.Designer.cs">
      <DependentUpon>Register.cs</DependentUpon>
    </Compile>
    <Compile Include="ResetPassword.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="ResetPassword.Designer.cs">
      <DependentUpon>ResetPassword.cs</DependentUpon>
    </Compile>
    <Compile Include="RestoreDefault.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="RestoreDefault.Designer.cs">
      <DependentUpon>RestoreDefault.cs</DependentUpon>
    </Compile>
    <Compile Include="SellerActivity.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="SellerActivity.Designer.cs">
      <DependentUpon>SellerActivity.cs</DependentUpon>
    </Compile>
    <Compile Include="EmpMethod.cs" />
    <Compile Include="SellerActivitySearchInventory.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="SellerActivitySearchInventory.Designer.cs">
      <DependentUpon>SellerActivitySearchInventory.cs</DependentUpon>
    </Compile>
    <Compile Include="Splash.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="Splash.Designer.cs">
      <DependentUpon>Splash.cs</DependentUpon>
    </Compile>
    <Compile Include="TaxDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="TaxDialog.Designer.cs">
      <DependentUpon>TaxDialog.cs</DependentUpon>
    </Compile>
    <Compile Include="TblCompanyProfile.cs" />
    <Compile Include="TblInventory.cs" />
    <Compile Include="TblRegister.cs" />
    <Compile Include="TblSales.cs" />
    <Compile Include="TblSellers.cs" />
    <Compile Include="TblSettings.cs" />
    <Compile Include="UpdateDelEmpDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="UpdateDelEmpDialog.Designer.cs">
      <DependentUpon>UpdateDelEmpDialog.cs</DependentUpon>
    </Compile>
    <Compile Include="UpdateDelInventoryDialog.cs">
      <SubType>Form</SubType>
    </Compile>
    <Compile Include="UpdateDelInventoryDialog.Designer.cs">
      <DependentUpon>UpdateDelInventoryDialog.cs</DependentUpon>
    </Compile>
    <EmbeddedResource Include="AddEmployeeDialog.resx">
      <DependentUpon>AddEmployeeDialog.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="AddItemDialog.resx">
      <DependentUpon>AddItemDialog.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="BusinessProfile.resx">
      <DependentUpon>BusinessProfile.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="DeleteAlldata.resx">
      <DependentUpon>DeleteAlldata.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="ForgottenPassword.resx">
      <DependentUpon>ForgottenPassword.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="GenerateBarCode.resx">
      <DependentUpon>GenerateBarCode.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="GenerateBarCodeDialog.resx">
      <DependentUpon>GenerateBarCodeDialog.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="OutOfStock.resx">
      <DependentUpon>OutOfStock.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="PremiumActivation.resx">
      <DependentUpon>PremiumActivation.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="RecoverAccount.resx">
      <DependentUpon>RecoverAccount.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="MainActivity.resx">
      <DependentUpon>MainActivity.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="Login.resx">
      <DependentUpon>Login.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="PrintMyCart.resx">
      <DependentUpon>PrintMyCart.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="Properties\Resources.resx">
      <Generator>ResXFileCodeGenerator</Generator>
      <LastGenOutput>Resources.Designer.cs</LastGenOutput>
      <SubType>Designer</SubType>
    </EmbeddedResource>
    <Compile Include="Properties\Resources.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Resources.resx</DependentUpon>
      <DesignTime>True</DesignTime>
    </Compile>
    <EmbeddedResource Include="Register.resx">
      <DependentUpon>Register.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="ResetPassword.resx">
      <DependentUpon>ResetPassword.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="RestoreDefault.resx">
      <DependentUpon>RestoreDefault.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="SellerActivity.resx">
      <DependentUpon>SellerActivity.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="SellerActivitySearchInventory.resx">
      <DependentUpon>SellerActivitySearchInventory.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="Splash.resx">
      <DependentUpon>Splash.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="TaxDialog.resx">
      <DependentUpon>TaxDialog.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="UpdateDelEmpDialog.resx">
      <DependentUpon>UpdateDelEmpDialog.cs</DependentUpon>
    </EmbeddedResource>
    <EmbeddedResource Include="UpdateDelInventoryDialog.resx">
      <DependentUpon>UpdateDelInventoryDialog.cs</DependentUpon>
    </EmbeddedResource>
    <None Include="Properties\Settings.settings">
      <Generator>SettingsSingleFileGenerator</Generator>
      <LastGenOutput>Settings.Designer.cs</LastGenOutput>
    </None>
    <Compile Include="Properties\Settings.Designer.cs">
      <AutoGen>True</AutoGen>
      <DependentUpon>Settings.settings</DependentUpon>
      <DesignTimeSharedInput>True</DesignTimeSharedInput>
    </Compile>
    <None Include="Shop POS_TemporaryKey.pfx" />
  </ItemGroup>
  <ItemGroup>
    <None Include="App.config" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="images\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <Content Include="logo\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
  <ItemGroup>
    <Content Include="favicon.ico" />
    <Content Include="images\alfa_romeo.jpg" />
    <Content Include="logo1_15W_icon.ico" />
    <Content Include="logo\DAEWOO.jpg" />
    <Content Include="Properties\add.png" />
    <None Include="Resources\za.png" />
  </ItemGroup>
  <ItemGroup>
    <BootstrapperPackage Include=".NETFramework,Version=v4.5">
      <Visible>False</Visible>
      <ProductName>Microsoft .NET Framework 4.5 %28x86 and x64%29</ProductName>
      <Install>true</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
    <BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
      <Visible>False</Visible>
      <ProductName>.NET Framework 3.5 SP1</ProductName>
      <Install>false</Install>
    </BootstrapperPackage>
  </ItemGroup>
  <ItemGroup />

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

标签: c#visual-studiopublish

解决方案


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

由于您正在复制图像文件夹,因此您不需要执行子字符串。

试试这个:

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

你能修改你的csproj文件吗?

<ItemGroup>
  <Content Include="images\alfa_romeo.jpg">
     <CopyToOutputDirectory>Always</CopyToOutputDirectory>
  </Content>    
</ItemGroup>

并删除您现有的

  <ItemGroup>
    <Content Include="images\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

这行:

在此处输入图像描述


推荐阅读