C# .NET - get the Owner of the file created through C#

Asked By santoshk kumar on 30-Jun-08 10:09 AM

i want  to get all the properties of the file  (ex:a txtfile or word file or excel file any file)

properties like (time created,size of the file,etccccccccc)

how to get the owner of the file i.e who  created the file (not the current login user )

many users will be their for a system

if a file is created by user A

and now B is login  now i want to chek who created the file from this (B) account

please asssisit me



file info - Partha Mandayam replied to santoshk kumar on 30-Jun-08 10:12 AM

You can find all file info using WMI.

http://www.softinsight.com/bnoyes/PermaLink.aspx?guid=8edc9d4c-0f2c-4006-8186-a3697ebc7476

hi - alice johnson replied to santoshk kumar on 30-Jun-08 01:14 PM

You can make use of the file watcher system to acheive this task.

FileSystemWatcher fsw = new FileSystemWatcher();
fsw.Path = textBox1.Text;
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
                   NotifyFilters.DirectoryName | NotifyFilters.FileName;
fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnCreated);
fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.Renamed += new RenamedEventHandler(OnRenamed);
fsw.EnableRaisingEvents = true;

See these links... they will help u understand better:

http://www.codeproject.com/KB/cs/fswatcher.aspx

http://msdn.microsoft.com/en-us/library/ms171383.aspx

See this code; - Sagar P replied to santoshk kumar on 30-Jun-08 11:43 PM

private string GetFileOwner(string path)

{

ManagementObject mgmt = new ManagementObject("Win32_LogicalFileSecuritySetting.path='" + path + "'");

ManagementBaseObject secDesc = mgmt.InvokeMethod("GetSecurityDescriptor",null,null);

ManagementBaseObject descriptor = secDesc.Properties["Descriptor"].Value as ManagementBaseObject;

ManagementBaseObject owner = descriptor.Properties["Owner"].Value as ManagementBaseObject;

return owner.Properties["Domain"].Value.ToString() + "\\" + owner.Properties["Name"].Value.ToString();

}

Best Luck!!!!!!!!!!!!!!!!!!!
Sujit.

Use this code snipet - Nadaraja Yogendran replied to santoshk kumar on 02-Jul-08 12:14 AM
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;


namespace ConsoleApplication1
{

 class GetFileProperty
 {

  static void Main(string[] args)
  {

            FileInfo fileInfo = new FileInfo (@"C:\Contacts.txt");
            FileSecurity fileSecurity = fileInfo.GetAccessControl();
            IdentityReference identityReference = fileSecurity.GetOwner(typeof(NTAccount));
            Console.WriteLine(identityReference.Value);
            Console.ReadLine();
                                   
   }
 }

}