简体   繁体   English

如何在C#代码中嵌入文件的内容?

[英]How to embed content of the file in the C# code?

Problem 问题

I have a file which contains such text (nothing else) 我有一个包含这样的文本的文件(没有别的)

2010-10-05 2010-10-05

I would like to embed this file (content) in the C# code, so I could assign this text to constant (string MyDate), and later use this constant. 我想将此文件(内容)嵌入到C#代码中,因此我可以将此文本分配给常量(字符串MyDate),然后使用此常量。

Background 背景

What I try to achieve is keeping information about the program in sync -- both on website and within program. 我试图实现的是保持程序的相关信息 - 无论是在网站上还是在程序内。

The solution has to be: 解决方案必须是:

  • dead simple 死得很简单
  • automatic 自动
  • smooth 光滑

The simplest I thought of, would be keeping file "version" with version (actually release date, because it is more meaningful IMHO), such file can be displayed on website with no problem (PHP). 我想到的最简单的,就是保持文件“版本”与版本(实际发布日期,因为它更有意义恕我直言),这样的文件可以在网站上显示没有问题(PHP)。 I think I manage writing tiny .bat file to auto-generate such file on each build. 我想我管理写的.bat文件很小,可以在每次构建时自动生成这样的文件。

But how to use it (embed) in C# code? 但是如何在C#代码中使用它(嵌入)?

I would like to achieve exactly this, or alternative solution but no more complex than this one. 我想要实现这个或替代解决方案,但不比这个更复杂。

Workarounds 解决方法

I already know several workarounds, like: 我已经知道了几个解决方法,比如:

  • creating not only file with just date, but small C# piece of code, which could be directly compiled 不仅可以创建具有日期的文件,还可以创建可以直接编译的小型C#代码
  • including this file in installer and after installation reading it, to get the version info 在安装程序中包含此文件,并在安装后读取它,以获取版本信息

But both do not feel "clean" too me. 但是我也不觉得“干净”。 I need something more smooth :-) 我需要更顺畅的东西:-)

Background of the background 背景的背景

My program should notify user "hey, there is newer version of the program" on one hand, on the other hand when browsing website info about release date should be displayed on page. 我的程序一方面应该通知用户“嘿,有更新版本的程序”,另一方面当浏览网页时,应该在页面上显示有关发布日期的信息。

Facts 事实

  • version: date of build which is also release date 版本:构建日期,也是发布日期
  • website: Linux, HTML & PHP 网站:Linux,HTML和PHP
  • program: VS 2010, C# 4.0, single exe 程序:VS 2010,C#4.0,单个exe
  • installer: Advanced Installer 安装程序:高级安装程序

Edit -- solution 编辑 - 解决方案

I tried to avoid the problem completely and rely on timestamps of the files -- it could work, because only one weak point would be the case when you build exe before midnight and create installation package after. 我试图完全避免这个问题,并依赖文件的时间戳 - 它可以工作,因为当你在午夜之前构建exe并在之后创建安装包时,只有一个弱点。 However, uploading files to ftp server ruins such plan -- you have to make sure ftp server support timestamp modification and you cannot use SFTP protocol. 但是,将文件上传到ftp服务器这样的计划 - 你必须确保ftp服务器支持时间戳修改,你不能使用SFTP协议。

So, I ended up creating "version" file and not embedding it (because I don't know how) -- I read the content both by PHP script on website and by C# code when executing. 所以,我最终创建了“版本”文件而没有嵌入它(因为我不知道如何) - 我在网站上用PHP脚本和执行时用C#代码读取内容。

Thank you in advance for your help. 预先感谢您的帮助。

If you use a resource file (.resx files) it can be compiled into the assembly. 如果使用资源文件(.resx文件),则可以将其编译到程序集中。

MSDN: Adding and Editing Resources (Visual C#) MSDN: 添加和编辑资源(Visual C#)

To solve the issue of checking for updates you could: use a WebClient to load a page from your site, and check the version information. 要解决检查更新的问题,您可以:使用WebClient从您的站点加载页面,并检查版本信息。

To solve the issue of displaying version information, you could simply leave a text file called versioninfo.txt in your installer, and load/display that at run time, or as stated by @Daniel you can use a resouce to compile the text file into the binary. 要解决显示版本信息的问题,您只需在安装程序中保留一个名为versioninfo.txt的文本文件,然后在运行时加载/显示该文件,或者如@Daniel所述,您可以使用资源将文本文件编译为二进制。

There seems to be two parts to your question: 您的问题似乎有两个部分:

1) Automatically generate a build number/version for your program 1)自动为您的程序生成内部版本号/版本

2) Have a web service that will check to see if the build number is the latest version. 2)有一个Web服务,它将检查内部版本号是否是最新版本。

For part 1, there are multiple solutions such as automatically including files via resources and pre-build events or even using MS automatically incrementing build numbers. 对于第1部分,有多种解决方案,例如通过资源自动包含文件和预构建事件,甚至使用MS自动递增构建号。

The second part involves sharing that information with your website. 第二部分涉及与您的网站共享该信息。 That would have to occur whenever you "publish" your latest version of program and is probably a manual event. 只要您“发布”最新版本的程序并且可能是手动事件,就必须发生这种情况。 You could write a "publishing" program that reads the MS build number or embedded resource of your program and writes that to what ever you will use as the back end store for your web service. 您可以编写一个“发布”程序来读取程序的MS内部版本号或嵌入式资源,并将其写入您将用作Web服务的后端存储的内容。

The web service then fields requests from the installed programs and checks to see if a later version exists. 然后,Web服务对来自已安装程序的请求进行转换,并检查是否存在更高版本。

edit 编辑

Thinking about it some more. 再思考一下。 All you need is the MS build number that increments each time you build the program. 您只需要每次构建程序时增加的MS内部版本号。 Think of that as the key to indexing into data kept on your website. 将其视为索引保存在您网站上的数据的关键。 The program sends the version number to the web service and if there is a newer version then the web service returns the newer version number plus its publication date. 程序将版本号发送到Web服务,如果有更新版本,则Web服务返回较新的版本号加上其发布日期。

However you may still want to embed a publication date in your program in order to display it in an about box without the need to go to the web service to find that information. 但是,您可能仍希望在程序中嵌入发布日期,以便将其显示在about框中,而无需转到Web服务来查找该信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM