473,503 Members | 1,633 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File::GetAttributes Is a file read only

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

Nov 17 '05 #1
4 2639
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
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.


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

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #2

Doug Harrison [MVP] wrote:
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
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.


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

--
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

Nov 17 '05 #3
On 28 Mar 2005 09:29:56 -0800, xenny wrote:
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).


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.

--
Doug Harrison
Microsoft MVP - Visual C++
Nov 17 '05 #4
On 28 Mar 2005 09:29:56 -0800, "xenny" <xe*****@hotmail.com> wrote:

Doug Harrison [MVP] wrote:
On 28 Mar 2005 06:49:49 -0800, xenny wrote:
> 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.


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

--
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).


All you need is:

if (fa & ReadOnly) {
}

or

if (File::GetAttributes(exportfile.c_str()) & ReadOnly) {
}
--
Sev
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
55379
by: Andy Gilman | last post by:
I want a method basically that tells me if a path is a file or directory, or any other possibilities. What do i need to do? I'm hoping I dont have to manually determine all these things with...
1
10659
by: John Grandy | last post by:
How to remove "Read Only" status on a file on a remote server? My goal is to delete the file, but System.File.Delete(\\machine1\c$\folder1\file1.txt) is giving me an "Access is denied"...
1
1569
by: Blue Man | last post by:
Anybody has idea how to save value in web.config file during runtime? ConfigurationSettings.AppSettings.Add("test","test value"); won't work, it will cause and error : the file is read only...
2
3074
by: Jarod_24 | last post by:
What is wrong here. This thing allways returns true. I want a simple way to see if a file is write protected. 'strFilename' holds the path for the file Dim isWriteProtected As Boolean =...
4
5511
by: Debbie Carter | last post by:
Would anyone know how to read the file attributes of a file on the hard drive to check if the file is encrypted or not? I have searched Visual Studio Help and MSDN and the samples I have found do...
1
6608
by: smay | last post by:
I need to make sure a file in my site's directory structure is not read-only. The following code runs after I grant permission to ASPNET on the folder, but the attribute is not removed (no...
6
6145
by: =?Utf-8?B?V2F5bmUgR29yZQ==?= | last post by:
Hi I want to achive 2 things. First I would like to compress an existing file on my harddrive. I can easily find out if a file is compressed or not by using "File.GetAttributes". But no matter...
3
4067
by: Mayur H Chauhan | last post by:
All, I am working on one product where we need to check for physical file is in use (if it is open by other application). What I found out is that I am able to trap for those files whose attribute...
1
4273
by: rcook349 | last post by:
From within my project, I am opening an file elsewhere on the network and changing contents within. When I go to close/save, however, I can't because it's read-only (as it's checked in). Can...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7460
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.