For Programmers: Free Programming Magazines  


Home > Archive > C# > March 2005 > file is writable...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author file is writable...
gloria

2005-02-26, 8:58 pm

Hey kids,

I am looking at FileIOPermissions to determine if a file is writable.
I can use try and catch to catch the instance where you can't write to
the file. But I can't determine that in advance. I would think I
could use FileIOPermissions to do this, but I can't figure it out.

I want to do something like:
filepermissionsValue = getfilepermissions(filename);
if (filepermssionsValue == "w")
print good;
else
print bad;

Could someone please give me an example of checking the permissions of
a file?

Thanks!

--gloria

gloria

2005-02-28, 3:59 pm

I determined that catching it after the fact is okay. I will just look
for that exception in the message.

Thanks!

Peter Mancini

2005-03-02, 3:58 am

Gloria, one thing I did on a project in December was to test a file to
see if it was currently being used by another program. What I did was
I attempted to put an exclusive lock on it. If that failed I knew
something else had the file open. If it didn't fail then I was certain
the file was mine to use.

Here is the function I used. Replace my variables for the file name on
your end. As you determined, exception catching works fine in this
situation (as it is meant to):

private bool TestFile()
{
FileStream fs;
try
{
fs = new FileStream(WatchFilePath+"\\"+WatchFile,FileMode.Open,
FileAccess.ReadWrite, FileShare.None);
}
catch
{
return false;
}
fs.Close();
return true;
}

gloria

2005-03-08, 8:59 pm

Cool. I will use this as an base. Thanks Peter!

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com