1

I am having an issue that is really killing me.

I have a directory that when I go to the properties window, shows Read-Only as partially checked (not a full check box, but the box is filled).

So I looked in the directory and I checked all the files, none of them have the read-only attribute. Only the folder has it, and only partially.

I tried the following code:

if (directoryInfo.Exists)
{
    try
    {
        directoryInfo.Attributes &= ~FileAttributes.ReadOnly;

        foreach (FileInfo f in directoryInfo.GetFiles())
        {
           f.IsReadOnly = false;
        }
    }
    catch (Exception e)
    {
        throw e;
    }
}

It still did not work. I can right click on the folder and manually remove the read-only permissions but I need to be able to do this in code. The code executes but does not error.

Anyone have any idea what the issue could be? My only guess is because the folder is on a network share (in the form of \\computer\folder\subfolder), that I might need special rights in order to change permissions on a folder?

Please someone help.

Thanks in advance

Issa Fram
  • 2,556
  • 7
  • 33
  • 63
  • Doing it on network is going to be harder due to security restrictions. You can try this though: http://stackoverflow.com/questions/2510565/changing-permissions-on-child-folders-in-c – Mrchief Jul 20 '11 at 17:41
  • I don't know if this can help you.. http://support.microsoft.com/kb/256614 – 2GDev Jul 20 '11 at 17:44
  • @Mrchief but why can I do it through Windows Explorer then? Am i impersonating another user or is it because I also have administrator privileges on my box? I think you are onto something but something doesn't make sense – Issa Fram Jul 20 '11 at 17:49
  • You're right. Locally you're an admin and have higher trust level. Network access by default runs on low trust. So in addition to having proper access rights, you also have to go through windows AD services for your code to work. http://www.redmondpie.com/applying-permissions-on-any-windows-folder-using-c/ – Mrchief Jul 20 '11 at 17:51
  • seem to be a Bug... i've created a new directory on windows 7 (from command prompt md c:\test) and read-only check is filled... try to change it but nothing happened! still the check is here... filled with blue! – 2GDev Jul 20 '11 at 17:56
  • @2GDev I'm using Windows 7 as well. I wonder what it looks like on another machine (WinXP). Or maybe another attribute is using the read-only attribute as a flag perhaps for something else? I'm so lost on this – Issa Fram Jul 20 '11 at 18:03

1 Answers1

1

readonly on folders is used by Windows internally... if you really need to change it then is some work involved (Registry and changing alot of folders)... see http://support.microsoft.com/kb/256614/en-us

Why do you need to make that change ?

EDIT - some information on Powershell and TFS:

http://codesmartnothard.com/ExecutingPowerShellScriptsOnRemoteMachinesWithTFS2010AndTeamDeploy2010.aspx

http://blogs.msdn.com/b/yao/archive/2011/06/15/tfs-integration-pack-and-scripting-using-powershell.aspx

or try a normal "batch file" (.bat) with "attrib -r" on the folder

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • this folder is part of output generated by a TFS build server for a Visual Studio 2010 Solution. It is in no way a system folder/file. Is it possible that TFS sets the folder with the UseSystemForSystemFolders attribute? – Issa Fram Jul 20 '11 at 17:52
  • I am no expert on TFS... sorry... I am still wondering why you need make this change ? perhaps there is some other way to achiev the goal... – Yahia Jul 20 '11 at 17:57
  • Why? Because for this current solution, we have manual deployment instructions to change the permissions of a couple folders to remove the ReadOnly attribute. If we forget to, parts of the application fail at runtime because they need to be able to edit the files in that folder. – Issa Fram Jul 20 '11 at 18:01
  • If this is a folder created ny TFS then perhaps you can change the folder persmission by running a script (Powershell) as part of the TFS build process (directly on the TFS server, no network mount). – Yahia Jul 20 '11 at 18:05
  • give me a link or something man. Some more information please. I tried using the above code in the TFS build workflow but its not working as you can see. Maybe Powershell will be different. – Issa Fram Jul 20 '11 at 18:07
  • attrib does not work on UNC paths i think. when trying to CHDIR with CMD i get "CMD does not support UNC paths as current directories" – Issa Fram Jul 20 '11 at 19:16
  • that is right - that is why I wrote you should run it directly on the TFS server (not via network) – Yahia Jul 20 '11 at 19:25