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

Publish c# Project with Custom App Root folder in Visual Studio

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

这是用于检索图像并将其显示到我的应用程序中的代码。它在发布应用程序之前工作正常,但在发布之后,它 returns directory notfound 异常。

 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>

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

因为你复制的是images文件夹,所以不需要做substring。

试试这个:

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>

和这一行:

谢谢盖伊。最后我不得不以编程方式完成它并且它有效。我在我的应用程序启动时以编程方式添加了图像目录,效果非常好。

 bool exists = System.IO.Directory.Exists("images");

            if (!exists)
                System.IO.Directory.CreateDirectory("images");