?
Solved

VB.net Windows 7 Access denied reading a text file

Posted on 2011-09-08
6
Medium Priority
?
692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

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…
IF you are either unfamiliar with rootkits, or want to know more about them, read on ....
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

649 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