4

I'm a newbie to programming, and have only been working with standard console programs written with C#.

I am currently on an internship, and I've been asked to design a little Tool for them.

To be fair, the assignment is way over my ahead, and nothing like what I have previously made with C#.

The Tool basicly has to do the following:

User choses a folder to be searched.

Program checks all files in the folder, and all sub folders, and checks the Write-protection if not already checked.

Program sets read-only attribute on all files, if there is not currently.

If this is not the place to search for help, please disregard my question.

Thanks for reading.

JFBN
  • 69
  • 6
  • possible duplicate of [C# Test if user has write access to a folder](http://stackoverflow.com/questions/1410127/c-sharp-test-if-user-has-write-access-to-a-folder) – cbr Jan 13 '14 at 12:36
  • Thanks for all the answers. I'm afraid this is an assignment too large for me. I have only created very simple console applications, and I have never seen commands as "var", "attr" before - and when we've been working with C#, things as "Public void ______" etc. has already been set, so when you write that, I understand this is too complicated for me. I've tried to copy/paste the final code that Avi Turner wrote into my program, but it will not recognize commands as "var". Maybe I am writing it the wrong place, I don't know. – JFBN Jan 13 '14 at 12:53
  • Can you show your current code? Just paste it to Pastebin.com. `var` is a variable declaration, `attr` is the name if the variable and what comes after the equals sign is the declared value. – cbr Jan 13 '14 at 13:00
  • @JFBN I think you are discouraging yourself with no reason. For example var is simply just a shortcut and could be replaced with the type of object (e.g. string). I suggest you don't give up so quickly, and take your time to learn. The beginning is always harder. If you need some help on this first assignment, you are more than welcome to contact me via email: avi.turner111 at google's mail. – Avi Turner Jan 13 '14 at 13:02
  • http://pastebin.com/wxN3f2P1 That is a link to my current code. Maybe this kind of code is not to be used as a console program? I have basicly only had a 3 week period of basic C# in my school period so far, so all we have done yet is basic "WriteLine", "SetCursorPosition", "ReadLine" etc commands, to be used for silly Things. – JFBN Jan 13 '14 at 13:05
  • Are you using c# for this application? The var keyword is used to implicitly declare a type (C# 3 and above). The first line of Avi's code reads simply `FileAttributes attr = File.GetAttributes(path)`. – crunchy Jan 13 '14 at 13:06
  • The code showed in the pastebin above is simply a copy of @AviTurner's linked code from his comment below. I do not understand much of it, I just tried it to see if it worked (which it did not, but that is likely because I am using it wrong). – JFBN Jan 13 '14 at 13:07
  • @JFBN I have refactored my answer in order to make it a bit more readable for you. Also, it looks like you are using VB not C#. You might want to read [this](http://www.harding.edu/fmccown/vbnet_csharp_comparison.html)... – Avi Turner Jan 13 '14 at 13:17
  • @AviTurner I just realised that I was actually using VB and not C#..... Stupid me. Usually when I create a new project, it's set to use C#, but I am on a new PC, so this time it was not set as this. Thank you so much for your constructive answers. I appreciate you taking your time to help me. – JFBN Jan 13 '14 at 13:22
  • @AviTurner I am getting two errors when trying to run the program. "The name 'Directory' does not exist in the current context" "The name 'SearchOption' does not exist in the current context" – JFBN Jan 13 '14 at 13:29
  • @JFBN this means you are missing their `namespace`. Try replacing `SearchOption` with `System.IO.SearchOption` and `Directory` with `System.IO.Directory` – Avi Turner Jan 13 '14 at 13:33
  • @AviTurner This gave me even more errors. `The name 'File' does not exist in the current context` `The name 'FileAttributes' does not exist in the current context` I understand if you feel like you are wasting your time, and I completely understand if you do not wish to help me, considering that I don't know half of what you're explaining me. These issues are probably very easy for you - it's just that I have never encountered them before, as this type of code is something I have never worked with. – JFBN Jan 13 '14 at 13:41
  • @JFBN Don't worry about it. I posted my mail address above, send me your solution, it will be quicker this way... Note: I am not suggesting this because I think you cannot handle it yourself. I just think that it will be easier for you to have a working example as a reference for your first assignment... – Avi Turner Jan 13 '14 at 13:43

4 Answers4

8

This is pretty much a copy paste from this thread:

The complete code should look something like:

    public void SetAllFilesAsReadOnly(string rootPath)
    {
        //this will go over all files in the directory and sub directories
        foreach (string file in Directory.EnumerateFiles(rootPath, "*.*", SearchOption.AllDirectories))
        {
            //Getting an object that holds some information about the current file
            FileAttributes attr = File.GetAttributes(file);

            // set the file as read-only
            attr = attr | FileAttributes.ReadOnly;
            File.SetAttributes(file,attr);
        }
    }

Following your comments, Just for better understanding, let's break it into bits and pieces:

once you have the file path, create the file attribute object:

var attr = File.GetAttributes(path);

For the following, you might want to read a bit about enum flags and bitwise

this is how you set as Read only:

// set read-only
attr = attr | FileAttributes.ReadOnly;
File.SetAttributes(path, attr);

this is how you un-set as Read only:

// unset read-only
attr = attr & ~FileAttributes.ReadOnly;
File.SetAttributes(path, attr);

And for getting all files you can use:

 foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories))
    {
        Console.WriteLine(file);
    }
Community
  • 1
  • 1
Avi Turner
  • 10,234
  • 7
  • 48
  • 75
2

You can check it via

FileAttributes attr = File.GetAttributes(path);
            if(attr.HasFlag( FileAttributes.ReadOnly ))
            {
             //it is readonly   
            }
Kamran Shahid
  • 3,954
  • 5
  • 48
  • 93
  • 1
    When you write "(path);", I am supposed to change that with the actual path of the file, yes? – JFBN Jan 13 '14 at 13:01
1

This MSDN thread introduces the following code sample for acquiring folder permissions:

DirectorySecurity dSecurity = Directory.GetAccessControl(@"d:\myfolder");
foreach (FileSystemAccessRule rule in dSecurity.GetAccessRules(true, true, typeof(NTAccount)))
{
 if (rule.FileSystemRights == FileSystemRights.Read)
 {
  Console.WriteLine("Account:{0}", rule.IdentityReference.Value);
 }
}
Ilya Tereschuk
  • 1,204
  • 1
  • 9
  • 21
0

Check out DirectoryInfo. In particular the Attributes property.

poy
  • 10,063
  • 9
  • 49
  • 74