Solved

VB.net Windows 7 Access denied reading a text file

Posted on 2011-09-08
6
662 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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: Lee
Windows 7 Ultimate and Enterprise (and 2008 R2) introduced a new feature you may not be aware of - Boot from VHD.   Boot from VHD (or what Microsoft refers to asNative Boot allows you to install Windows to a VHD (Virtual Hard Disk) file that is t…
First some basics on Windows 7 Backup.  It has 2 components one is a file based backup which is stored in .zip files each zip is split at around 200 Megabytes and there is the Image Backup which is as the name implies a total image of the partition …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now