如何在应用程序内部写入文本文件 [英] How to write to a text file inside of the application

查看:140
本文介绍了如何在应用程序内部写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的应用程序内部有一个文本文件(在应用程序属性的资源标签中).

Basically, I have a text file inside of my application (in the resources tab of the application's properties).

现在我正在尝试阅读&写入该文件,阅读就可以了,这是写作部分给了我麻烦.

Now I'm trying to read & write to that file, reading works just fine, it's the writing part that's giving me issues.

我正在使用dim str as string = my.resources.textfile,它可以正常工作.

I'm using dim str as string = my.resources.textfile, which works.

现在我正在尝试使用my.resources.textfile = str2,这给我一个错误,指出文件是只读的.

Now I'm trying to use my.resources.textfile = str2, which gives me an error stating the file is Read only.

我该如何解决?

注意:我不喜欢将该文件写入用户的PC,因为它不需要写入大量数据,并且(在我看来)因为它看起来有点不专业,所以我更喜欢这样做的方法无需将文件写入用户的PC.

Note: I don't like writing the file to the user's PC as it's not a lot of data which needs to be written and because it looks a tad unprofessional (in my eyes), so I prefer a way to do this without writing a file to the user's PC.

推荐答案

当它只是一个很小的小文本文件(包含1行文本)时,将其写入另一个文件是一种浪费.

when it's just a tiny little text file containing 1 line of text, it's kind of a waste to write it into a different file.

那为什么会是废物"?这是一种奇怪的看待方式.如果首先值得将文件写入,则值得将其写入单独的文件.

Why would that be a "waste"? That's a strange way of looking at it. If it's worth it to write the file in the first place, it's worth it to write it to a separate file.

就像雅各布在评论中说的那样,修改可执行文件本身是一项不平凡的任务.使代码起作用以实现该目标将是真正的浪费.并且假定您可以通过病毒扫描程序,公司策略或基本代码审查获得类似的代码.

Like Jacob says in a comment, modifying the executable itself is an utterly non-trivial task. Getting the code working to make that happen would be the real waste. And that assumes you can get code like that past a virus scanner, corporate policy, or basic code review.

我不喜欢将文件写入用户的PC,因为它不需要写入大量数据,并且(在我眼中)看起来有点不专业,所以我更喜欢一种无需写入的方法一个文件到用户的PC.

I don't like writing the file to the user's PC as it's not a lot of data which needs to be written and because it looks a tad unprofessional (in my eyes), so I prefer a way to do this without writing a file to the user's PC.

担心这个问题具有正确的直觉,但是在这种情况下,担心的地方是错的.将文件写入用户的桌面,文档文件夹甚至硬盘的根目录确实是不专业的.这些位置要么完全属于用户,要么属于系统,即使您可以成功地写入它们(UAC也会在磁盘的根级别上与您进行全面斗争),但您不应该这样做. ( Blah blah blah,我之前曾对此大喊大叫.)

You have the right instincts in worrying about this, but the worry in this particular case is misplaced. It would indeed be unprofessional to write files to the user's desktop, or documents folder, or even the root level of the hard disk. These locations either belong exclusively to the user or the system, and even if you could successfully write to them (UAC will fight you all the way on the root level of the disk), you shouldn't. (Blah blah blah, I've ranted about this before.)

相反,请使用专门用于此目的的Application Data文件夹.您将获得对该位置的读/写权限,并且没有普通用户看过该位置,因此他们不会看到您扔进去的任何东西.查看该位置的异常用户会期望这种东西存放在那里.

Instead, use the Application Data folder that is intended for exactly this purpose. You're guaranteed to have read/write privileges to this location, and no normal user ever looks there so they won't see any of the stuff you toss in. Abnormal users who look there expect to see this kind of stuff stored there.

您唯一可能犯的错误是将硬路径编码为此类文件夹.不要那样做-它会将位置从一台计算机更改为另一台计算机.而是使用 Environment.GetFolderPath 方法进行检索它的位置.该函数采用 Environment.SpecialFolder 值,其中有一个疯狂的数字.为此您感兴趣的三个是:

The only possible mistake that you could make would be to hard-code the path to such a folder. Don't do that—it changes location from one machine to the next. Instead, use the Environment.GetFolderPath method to retrieve its location. That function takes one of the Environment.SpecialFolder values, of which there are an insane number. The three you're interested in for this purpose are:

  • ApplicationData,用于存储应使用用户帐户(即,当他们使用另一台计算机登录时使用的帐户)漫游的应用程序数据
  • LocalApplicationData,用于存储不应该使用用户帐户漫游的应用程序数据(即,仅在当前计算机上本地存储)
  • CommonApplicationData,用于存储所有用户通用的应用程序数据(即非特定于用户的数据).
  • ApplicationData, which is used to store application data that should roam with the user's account (i.e., go with the account when they log on using a different machine)
  • LocalApplicationData, which is used to store application data that should not roam with the user's account (i.e., stay locally only on the current machine)
  • CommonApplicationData, which is used to store application data that is common to all users (i.e., non-user-specific).

这篇关于如何在应用程序内部写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆