user2051823
user2051823

Reputation: 177

vb6 read and write a text file in win 7

I am trying to execute a .exe file (created by VB6) on Windows 7. What the application will do is to simply create a text file, and then read the text for a text file. All the functions are being tested successfully on Windows XP. When I execute the .exe file on Win 7, the functions act as usual but the file I created doesn't exist on the specific path (C:\test.txt). The most weird thing is that I can still read the content from the text file (through the .exe file) despite being unable to find the text file on file explorer.

Then I discovered that I have to choose [run as Administrator] to execute the .exe file, so that the file (test.txt) will be created on the C: drive. I am very curious how the .exe file can still read the text file even it is not existed, and how can I force the .exe file to run as Administrator?

Here is the coding to write and read a file.

Open "C:\" & "test.txt" For Output As #1
Print #1, cDrive.Text
Close #1


Open "C:\" & "test.txt" For Input As #1
Input #1, msg
Close #1
cDrive.Text = msg
Exit Sub

Upvotes: 0

Views: 1739

Answers (3)

Emping
Emping

Reputation: 11

To answer the third remark: Windows Vista and Windows 7 User Access Control (UAC) introduced a feature called the VirtualStore which is designed to add an extra layer of security protection for applications installed under the Program Files folder. If you search for the file on you hdd you might find a second instance of the file in /User/AppData/Local/VirtualStore So that's why it is still able to read text.txt allthough is doesn't excist in the location you mentioned.

Upvotes: 1

Guy Cohen
Guy Cohen

Reputation: 759

  1. I suggest that you run the program as an administrator by using the feature in the shortcut (after compiling and installing).
  2. If you asked about it when in debug mode - I believe that if the user that you use to debug is an admin - it's enough.
  3. I have no answer for your question "I am very curious how the .exe file can still read the text file even it is not existed"
  4. You should not specify open as #1 directly, use FreeFile() function instead Look here for a sample.

Good luck

Upvotes: 0

George
George

Reputation: 2213

Drive C is being protected by Win7, you can still write to it, but you really shouldn't.

If you want to run the app as admin:

  • right click on the EXE
  • select PROPERTIES
  • go into COMPATABILITY tab
  • check RUN THIS PROGRAM AS ADMINISTRATOR check box
  • click OK

Now every time you run the app, it will run as administrator

Upvotes: 0

Related Questions