Гость
Целевая тема:
Создать новую тему:
Автор:
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Получить логин владельца файла. / 5 сообщений из 5, страница 1 из 1
04.06.2013, 12:26
    #38284972
Compositum
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Получить логин владельца файла.
Доброго времени суток.

Задача: получить имена (логины) владельцев указанных файлов. Все пользователи доменные.

Написал такой код:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
using System;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace get_owner {
    class Program {
        static void Main(string[] args) {
            foreach (String item in args) {
                try {                    
                    FileSecurity fs = File.GetAccessControl(Environment.ExpandEnvironmentVariables(
                    args[0]));
                    IdentityReference account = fs.GetOwner(typeof(System.Security.Principal
                        .NTAccount));               
                    Console.WriteLine("Object: {0}", item);                    
                    Console.WriteLine("Owner: {0}", account.Value);
                    Console.WriteLine();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception for the '{0}':", item);
                    Console.WriteLine("Exception message: {0}", e.Message);
                    Console.WriteLine();
                }
                
            }            
            Console.Write("Press any key for exit...");
            Console.ReadKey();
        }
    }
}


Результат получаю следующий:
Вывод на консольObject: C:\Program Files\Autodesk\AutoCAD 2009\Support\acad2009doc.lsp
Owner: NT AUTHORITY\SYSTEM

Object: C:\test_data\tr_ext.lsp
Owner: NT AUTHORITY\SYSTEM

Press any key for exit...

Однако, на самом деле в качестве владельца выводится не то, что мне нужно... Например, применительно ко второму файлу, я хочу увидеть такое значение:



Что нужно сделать, чтобы в качестве владельца вместо " NT AUTHORITY\SYSTEM " возвращало ожидаемое мною " HYPROSTROY\andrey.bushman "?

Спасибо.
...
Рейтинг: 0 / 0
04.06.2013, 12:35
    #38284994
МСУ
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Получить логин владельца файла.
Код: c#
1.
IdentityReference account = fs.GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount));
...
Рейтинг: 0 / 0
04.06.2013, 12:41
    #38284998
Compositum
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Получить логин владельца файла.
МСУ
Код: c#
1.
IdentityReference account = fs.GetOwner(typeof(SecurityIdentifier)).Translate(typeof(NTAccount));


К сожалению, результат остаётся прежним. :(
...
Рейтинг: 0 / 0
04.06.2013, 12:58
    #38285041
Compositum
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Получить логин владельца файла.
Прошу прощенья, нашёл ошибку в своём коде (в цикле всё время считывал args[0], вместо args[i]. Т.о. всё работает. Спасибо за помощь, Даниил!
...
Рейтинг: 0 / 0
04.06.2013, 13:01
    #38285053
Compositum
Участник
Скрыть профиль Поместить в игнор-лист Сообщения автора в теме
Получить логин владельца файла.
Т.е. правильный вариант такой:
Код: c#
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
using System;
using System.DirectoryServices.AccountManagement;
using System.IO;
using System.Security.AccessControl;
using System.Security.Principal;

namespace get_owner {
    class Program {
        static void Main(string[] args) {            
            for(int i = 0; i < args.Length; ++i) {
                try {                    
                    FileSecurity fs = File.GetAccessControl(Environment.ExpandEnvironmentVariables(
                    args[i]));
                    IdentityReference account = fs.GetOwner(typeof(System.Security.Principal
                        .NTAccount)); 
                    // или так:
                    // IdentityReference account = fs.GetOwner(typeof(SecurityIdentifier))
                    //  .Translate(typeof(NTAccount));
                    Console.WriteLine("Object: {0}", args[i]);
                    Console.WriteLine("Owner: {0}", account.Value);
                    Console.WriteLine();
                }
                catch (Exception e) {
                    Console.WriteLine("Exception for the '{0}':", args[i]);
                    Console.WriteLine("Exception message: {0}", e.Message);
                    Console.WriteLine();
                }
            }            
            Console.Write("Press any key for exit...");
            Console.ReadKey();
        }
    }
}
...
Рейтинг: 0 / 0
Форумы / WinForms, .Net Framework [игнор отключен] [закрыт для гостей] / Получить логин владельца файла. / 5 сообщений из 5, страница 1 из 1
Найденые пользователи ...
Разблокировать пользователей ...
Читали форум (0):
Пользователи онлайн (0):
x
x
Закрыть


Просмотр
0 / 0
Close
Debug Console [Select Text]