?
Solved

VB.net Windows 7 Access denied reading a text file

Posted on 2011-09-08
6
Medium Priority
?
687 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
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!

 

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

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
In this Micro Tutorial viewers will learn how to use Boot Corrector from Paragon Rescue Kit Free to identify and fix the boot problems of Windows 7/8/2012R2 etc. As an example is used Windows 2012R2 which lost its active partition flag (often happen…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
Suggested Courses

764 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