-2

I would like to get the file owner information but are having some trouble with this line:

const string fileOwner = Delimon.Win32.IO.File.GetAccessControl(fi.Name).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();

Specifically, it doesn't like fi.name. My error is

An object reference is required for the non-static field, method, or property 'Delimon.Win32.IO.File.GetAccessControl(string)'

Here is the larger chuck of code where it sits:

// check if file exists.  if so dont overwrite...
if(Delimon.Win32.IO.File.Exists(targetPath+fi.Name)) {
    // Console.WriteLine(fileName + " already exists, nothing written");
}
else {
    // Console.WriteLine(fileName + " is new so written to dir");
    string fileOwner=Delimon.Win32.IO.File.GetAccessControl(fi.Name).GetOwner(typeof(System.Security.Principal.NTAccount)).ToString();
    Delimon.Win32.IO.File.Copy(fileName, destFile, true);

    // convert extension to number
    switch(fileExt) {
        case ".doc":
            fileType=1;
            break;
        case ".xls":
            fileType=2;
            break;
        case ".mdb":
            fileType=3;
            break;
        default:
            fileType=1;
            break;
    }

    // I want tot use fileOwner here in an SQL statement
}

Please note fi.Name holds the file name only. I also have a variable called fileName which holds the full absolute path including the file name. This gives me the same issue.

Ken Kin
  • 4,503
  • 3
  • 38
  • 76
Mat41
  • 1,287
  • 4
  • 15
  • 29
  • 2
    Why tag with `console-application` and `asp.net`? And yes, you have a compiler error. And what is this `Delimon` stuff? – leppie Apr 03 '13 at 04:56
  • You've stored the `fileOwner`.. what's stopping you from using it in an SQL statement like you want? – Simon Whitehead Apr 03 '13 at 04:58
  • What is `Delimon`? A [Google search](http://www.google.com/search?q=delimon) was unhelpful. That's where your problem is, you need to show the code for the `Delimon.Win32.IO.File.GetAccessControl` function. – Cody Gray - on strike Apr 03 '13 at 05:03

3 Answers3

3

The method File.GetAccessControl() is 'implemented' as an instance methods and not a static method (not like the one on MSDN)

Try to create a file of type Delimon.Win32.IO.File then call the methode using the created instance:

Delimon.Win32.IO.File file  = new Delimon.Win32.IO.File(...); // Check constructor
FileSecurity fs = myFile.GetAccessControl();

You may want to check the documentation regards the Delimon.Win32.IO.File.Exists method which could be an instance methode either.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • @leppie Thank you for your comment. In response this is a .net console application, sorry to upset you with he tagging. I thoguht I was being through? yes I have the error as you point out, this is the whole point of my post, to get some help... FYI the Delimon stuff is a library is written specifically to overcome the limitation of the .NET Framework to use long Path & File Names. Rea about it here http://gallery.technet.microsoft.com/DelimonWin32IO-Library-V40-7ff6b16c#content – Mat41 Apr 03 '13 at 22:03
  • thank you for your comment. I am new to c# (a classic ASP guy converting). When I try your code it has an issue for me. FileSecurity cant find a type or a namespace? Also can I ask where you have the ... what is expected here? And should I place just te file name of the full path in the brackets around getAccesControl? Thanks in advance – Mat41 Apr 03 '13 at 22:21
  • Take a look at the method GetAccessControl() or its documentation, and declare the variable fs of the same type this method returns, i assumed it return a FileSecurity object liek the one on MSDN. According to the method declaration you may have to provide parameters to the methode (in the brackets), but i guess if there are any parameters they would not include the file path, since you call the method on a File object as you can see. you will need the path when you declare the File in the row above. – CloudyMarble Apr 04 '13 at 04:31
  • Doesnt work. I got an method not implemented with your example. `The method or operation is not implemented. at Delimon.Win32.IO.File.GetAccessControl(String path)` Example was: `File f = new File(); f.GetAccessControl(path);` – PatrickB Aug 22 '14 at 09:13
-1

From the error, it sounds like fi.Name is unable to be found because fi is null when calling GetAccessControl. Take a look at it in the debugger.

Chris Doggett
  • 19,959
  • 4
  • 61
  • 86
  • That's not what it sounds like to me. It looks like `Delimon.Win32.IO.File.GetAccessControl` is an instance method being called as a static one. – Cody Gray - on strike Apr 03 '13 at 05:04
  • @Simon Whitehead perhaps you didn't understand my post or I wasn't clear enough for you. fileOwner is where the issue is.... – Mat41 Apr 03 '13 at 22:06
  • Hi Chris - No fi does havea value, i have double checked this. As I have said in my initial post fi.Name holds the file name only and fileName holds the full absolute path including the file name – Mat41 Apr 03 '13 at 22:23
-1

Sorry for a couple of answers I placed in the wrong spot. Am new to this site (and c#/.net if you cant tell) I believe I have found a solution here Getting / setting file owner in C# Based on that this is how I have it working:

using System.Security.AccessControl;
using System.Security.Principal;

var fs = System.IO.File.GetAccessControl(fileName);
var sid = fs.GetOwner(typeof(SecurityIdentifier));
Console.WriteLine(sid);
var ntAccount = sid.Translate(typeof(NTAccount));
Console.WriteLine(ntAccount);

so ntAccount is may variable which I will use in the SQL for the document owners name. Thanks for every ones time helping me here. This solution does seem to be working. Quick question since im new here. Should I have posted this as a reply and should I accept it?

Community
  • 1
  • 1
Mat41
  • 1,287
  • 4
  • 15
  • 29