• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 458
  • Last Modified:

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

0
Uniqueinc
Asked:
Uniqueinc
  • 4
1 Solution
 
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now