【问题标题】:Publish c# Project with Custom App Root folder in Visual Studio在 Visual Studio 中使用自定义应用程序根文件夹发布 c# 项目
【发布时间】:2019-12-06 03:16:26
【问题描述】:

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

【问题讨论】:

  • 在发布时尝试将一些示例文件放入 images 文件夹中,因为空文件夹可能无法发布。
  • 感谢 Srikanth,我已经这样做了,我添加了两张图片,但错误仍然是他们的。图像路径无法识别。执行目录异常错误
  • 文件夹复制到输出目录了吗?如果是,我会比较文件夹结构 - 自定义文件夹的相对路径通常与本地开发框与发布后的内容不同。如果启用了日志记录,我将添加一个记录 imagePath(或路径)的日志语句。
  • 是的,它被复制了。另外,在发布应用程序后,我可以看到应用程序文件中的文件夹以及里面的文件。大问题是应用发布后检索图像的代码。

标签: c# visual-studio publish


【解决方案1】:

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

由于是复制图片文件夹,所以不需要子串。

试试这个:

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>

还有这一行:

【讨论】:

  • 感谢山姆的回复。它仍然返回相同类型的错误。
  • 您有机会记录 imagePath 吗?它是否存在于预期的位置(imagePath)?
  • Hae Sam ,您的回答是正确的,但找不到图像目录。我正在将图像上传到位于应用程序根目录的图像目录,在发布之前可以工作,但在发布之后,我不知道没有发生什么,我修改了 .csproj 以将图像目录复制到输出目录。请帮助我知道你可以帮助我,因为我能感觉到你有经验
  • 很抱歉,我无法回复您的消息,但注意到您自己解决了问题。
  • 我非常感谢这个小费......但我只是想知道为什么我不能像其他人一样使用 .csproj 来做到这一点,因为如果我在不同的计算机上发布应用程序,即使数据库也会出现问题.您能否给我一个提示,因为这是我在 C# 中发布的第一个应用程序,并且两个多星期以来都在努力发布它
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-30
    • 1970-01-01
    • 2015-02-22
    • 2017-10-25
    • 2010-11-14
    • 1970-01-01
    相关资源
    最近更新 更多