How to write to a text file inside of the applicat

2020-04-21 08:38发布

问题:

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.

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

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

How do I work around this?

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.

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.

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.)

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.

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, 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).


回答2:

if its just login credentials why not use My.Settings? Got your project properties and then to the settings tab, add your settings Eg. "username" etc and use that like: My.Settings.username = "Yorrick" My.settings.save