Solved

VB.net Windows 7 Access denied reading a text file

Posted on 2011-09-08
6
677 Views
Last Modified: 2012-05-12
Hi

I successfully created a text file using the code below but get the error
"access denied to C:......" no matter where write this file to. I must surely be usingf the wrong sort
of code. I have VB.net 2010 with Windows 7 installed
Public arrSettings(100) As String

    'The Program Files folder, and all sub folders, are read only to standard users.  This is by design.  Instead, use the application data folder:

    'My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData
    'or
    'My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData 

    Sub WriteText()

        Dim oPath As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\EI" 'Environ("userprofile") & "\EI"
        'Dim oPath As String = "C:\Program Files\EI"


        If Not Directory.Exists(oPath) Then
            Directory.CreateDirectory(oPath)
        End If

        File.WriteAllLines(oPath & "/Settings.txt", arrSettings)

    End Sub

    Sub ReadText()

        Dim oPath As String = My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData & "\EI" 'Environ("userprofile") & "\EI"

        If Not Directory.Exists(oPath) Then
            'Directory.CreateDirectory(oPath)
            Exit Sub
        End If

        ' Create StreamReader for the file.
        'Using reader As StreamReader = New StreamReader("file.txt")
        Using reader As StreamReader = New StreamReader(oPath)
            ' Do While true loop.
            Do While (True)
                ' Read a line.
                Dim line As String = reader.ReadLine
                ' See if line is Nothing.
                If line Is Nothing Then
                    Exit Do
                End If
                ' Write line to screen.
                'Console.WriteLine(line)
            Loop
        End Using
    End Sub

Open in new window

0
Comment
Question by:murbro
6 Comments
 
LVL 3

Expert Comment

by:Crower
ID: 36501177
a simple test. Create a folder in C:
For example "C: \ Test" and tries to create the file there.
Maybe it's because you're trying to write a system folder and you have to run the script as Administrator, or write the file to a route system that is not
0
 
LVL 39

Expert Comment

by:appari
ID: 36501189
also can you try changing the file name to something else. in windows 7 security is escalated if file names contains few keywords(undisclosed) like setup, install etc., may be "settings" is  also one of the keywords causing the security escalation.
0
 

Author Comment

by:murbro
ID: 36501210
Still doesn't work. This is bizarre
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Expert Comment

by:N0dlehs
ID: 36501448
have you tried modifying the execution level from require
administrator to asinvoker?
0
 
LVL 4

Accepted Solution

by:
Ambusy earned 500 total points
ID: 36502063
in Windows 7 (or Vista) put your own (not installed) files in Application data, which is a folder under your UserAccount directory. That one is readable and writable. great part of disk C:\ is protected from you by windos security.
0
 

Author Closing Comment

by:murbro
ID: 36504153
Thanks very much
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
This Micro Tutorial will give you basic overview of the control panel section on Windows 7. It will depth in Network and Internet, Hardware and Sound, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question