?
Solved

VB.net Windows 7 Access denied reading a text file

Posted on 2011-09-08
6
Medium Priority
?
699 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:Murray Brown
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:Murray Brown
ID: 36501210
Still doesn't work. This is bizarre
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 2000 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:Murray Brown
ID: 36504153
Thanks very much
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

850 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