Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2005

Tip: Looking for answers? Try searching our database.

File::GetAttributes Is a file read only

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
xenny - 28 Mar 2005 15:49 GMT
Hi,

I'm trying to see if a file (exportfile) is set to read only using
File::GetAttributes:

if ((File::GetAttributes(exportfile.c_str()) &
FileAttributes::ReadOnly) != 0);
{
    MessageBox::Show("The File is Read Only","Warning");
}

I've tried that and a few variations but it always returns true.

Any help would be greatly appreciated.

Thanks,

Chris
Doug Harrison [MVP] - 28 Mar 2005 18:00 GMT
> Hi,
>
[quoted text clipped - 10 lines]
>
> Any help would be greatly appreciated.

You have an errant semicolon. VC  warns about such "empty controlled
statements" at warning level 3 and above.

Signature

Doug Harrison
Microsoft MVP - Visual C++

xenny - 28 Mar 2005 18:29 GMT
> > Hi,
> >
[quoted text clipped - 17 lines]
> Doug Harrison
> Microsoft MVP - Visual C++

O yeah, good point :-)

But even if I remove that it still does the same thing.

But, I've found a way to make it work:

    FileAttributes fa = File::GetAttributes(exportfile.c_str());

    if ((fa == ReadOnly) || (fa % 2 == 1))
    {
        MessageBox::Show("The File is Read Only","Warning");
    }

Now that will only show the message box when the file is read only.
And will work even if the file has other attributes as well as the read
only (the second part of the if statement).

Cheers.

Chris
Doug Harrison [MVP] - 28 Mar 2005 19:39 GMT
> O yeah, good point :-)
>
[quoted text clipped - 12 lines]
> And will work even if the file has other attributes as well as the read
> only (the second part of the if statement).

That's not how to do it. The following should work:

FileAttributes attr = File::GetAttributes(exportfile.c_str());
if (attr == -1)
{
  // error
}
else if (attr & FileAttributes::ReadOnly)
{
  // File is readonly
}

You have to check it against -1 because that's what GetAttributes returns
in case of error, e.g. a file that doesn't exist.

Signature

Doug Harrison
Microsoft MVP - Visual C++

Severian - 28 Mar 2005 19:50 GMT
>> > Hi,
>> >
[quoted text clipped - 34 lines]
>And will work even if the file has other attributes as well as the read
>only (the second part of the if statement).

All you need is:

   if (fa & ReadOnly) {
   }

or

   if (File::GetAttributes(exportfile.c_str()) & ReadOnly) {
   }

--
Sev

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.