将多个背景图像文件添加到可部署项目中

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

我看了,没有看到解决我问题的答案。我有一个使用随机文件作为背景图像的应用程序。我目前将这些图像存储在本地计算机上的文件夹中,然后遍历文件夹以将图像文件名加载到ArrayList中,该工作正常。但是,当我将应用程序部署到另一台计算机时,我需要一种方法来使其工作。

那么:1)有没有办法用Visual Studio Publish功能部署这些图像,或2)有没有办法将它们添加到项目的Resources区域,然后将它们添加到Array(List)或者遍历它们?我在资源列表中确实有其他文件(图标等),我不想包含这些文件。我可以使用一系列的IF语句来排除它们,但这感觉很糟糕:)你们所有人的想法都会非常感激。

我目前在本地计算机上使用的代码:

    ArrayList bgImageList = new ArrayList();

    bgImageList = myIO.ProcessDirectory(bgImageFilePath);

    DisplayBackgroundImage();
    private void DisplayBackgroundImage()
    {
        Random rnd = new Random();
        int index = rnd.Next(0, bgImageList.Count);
        this.BackgroundImage = Image.FromFile((String)bgImageList[index]);
    }

class FileIO
{
    // Note: I think I found this code on this site. I cannot recall the author's name 
    // but all credit goes to them. I did modify it to return the ArrayList.

    ArrayList myList = new ArrayList();

    public ArrayList ProcessDirectory(string targetDirectory)
    {
        string[] fileEntries = Directory.GetFiles(targetDirectory);
        foreach (string fileName in fileEntries)
            ProcessFile(fileName);

        string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
        foreach (string subdirectory in subdirectoryEntries)
            ProcessDirectory(subdirectory);

        return myList;
    }

    private void ProcessFile(string path)
    {
        myList.Add(path);
    }
}
c# image resources
1个回答
0
投票

您可以将图像添加到项目中,然后在属性中将Copy to output directory设置为Copy to if newer这应该足以使Publish功能包括文件

如果没有,您可以编辑.pubxml文件添加新目标

  <Target Name="CustomCollectFiles">
    <ItemGroup>
      <_CustomFiles Include="*.jpg" />
      <FilesForPackagingFromProject Include="%(_CustomFiles.Identity)">
        <DestinationRelativePath>Resources\%(Filename)%(Extension)</DestinationRelativePath>
      </FilesForPackagingFromProject>
    </ItemGroup>
  </Target>

并复制文件:

  <PropertyGroup>
    <CopyAllFilesToSingleFolderForPackageDependsOn>CustomCollectFiles;
      </CopyAllFilesToSingleFolderForPackageDependsOn>
    <CopyAllFilesToSingleFolderForMsdeployDependsOn>CustomCollectFiles;
      </CopyAllFilesToSingleFolderForMsdeployDependsOn>
  </PropertyGroup>
© www.soinside.com 2019 - 2024. All rights reserved.