VB.Net - FileInfo.IsReadOnly is not returning that a file has been marked as ReadOnly

I'm trying to check and see if a file has been marked as Read only.  If it is Read Only, then I want to change to not be Read Only.  However, the program is not seeing that the IsReadOnly property = True.  It always thinks that IsReadOnly = false.  Am I doing something wrong?

I'm using .Net Framework 3.5.
Dim objFile as New FileInfo(full file path)
objFile.Refresh()
If objFile.IsReadOnly = True Then
	objFile.IsReadOnly = False
End If

Open in new window

UniqueincAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Reza RadConsultant, TrainerCommented:
code seems correct, are you sure that the file you check is readOnly really?
0
UniqueincAuthor Commented:
Yes, by 2 means:

1. I view the file's properties in Windows Explorer and see that it is marked as Read Only.

2. My code that follows the afore mentioned lines tries to modify the contents of the file and fails with a "The file is read only and cannot be modifed" (not exact quote) error.
0
lazyberezovskyCommented:
What is the way you are opening file for writing?
Are you sure you don't use FileAccess.Read?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

UniqueincAuthor Commented:
I'm looking for an exe file so that I can modify the connection string property in it's app.config file.
If UCase(objFile.Extension) = ".EXE" Then
	If File.Exists(objFile.FullName + ".config") Then
		Dim appconfig As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(objFile.FullName)
		Dim csSection As ConnectionStringsSection = appconfig.ConnectionStrings
		Dim csCollection As ConnectionStringSettingsCollection = csSection.ConnectionStrings
		Dim cs As ConnectionStringSettings = csCollection(0)
		cs.LockItem = False
		cs.ConnectionString = newConnectionString
		appconfig.Save()
	End If
End If

Open in new window

0
UniqueincAuthor Commented:
Never mind.  I figured out my problem.  I was only setting the IsReadOnly =False prop for the exe file and I was settings it the app.config file also.

Once I did that, it is working fine.  Thanks for helping me think this through!
0
UniqueincAuthor Commented:
His comment helped me think through my problem better and helped me to figure out the problem myself.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.