7 Replies - 620 Views - Last Post: 29 October 2012 - 05:46 AM Rate Topic: -----

#1 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Directory Access rights?

Posted 27 October 2012 - 01:38 PM

Hi guys,

I'm trying to build some Image viewer application, and i want to populate TreeView control with subdirectories and files in "C://" partition, so i write some code and i was had some "Access denied for bla bla directory", ok then i try to put Security directory where i try to allow access rights for "C://" partition, but now i have this error -> Some or all identity references could not be translated. ,
so what can to do?

Sorry for bad English language.

I use Windows 7 os.

this is C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Security.AccessControl;

namespace Image_Viewer_1._0
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            ListDirectory(treeView1,"C://");
        }

        private void ListDirectory(TreeView treeView, string path)
        {
            treeView.Nodes.Clear();
            var rootDirectoryInfo = new DirectoryInfo(path);
            treeView.Nodes.Add(CreateDirectoryNode(rootDirectoryInfo));
        }

        private TreeNode CreateDirectoryNode(DirectoryInfo directoryInfo)
        {
            AddDirectorySecurity(directoryInfo.FullName, "Srdjan-PC", FileSystemRights.ReadData, AccessControlType.Allow);
            var directoryNode = new TreeNode(directoryInfo.Name);
             foreach (var directory in directoryInfo.GetDirectories())
            directoryNode.Nodes.Add(CreateDirectoryNode(directory));
            foreach (var file in directoryInfo.GetFiles())
            directoryNode.Nodes.Add(new TreeNode(file.Name));
            return directoryNode;
        }

        public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
        {
            DirectoryInfo dInfo = new DirectoryInfo(FileName);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
            dInfo.SetAccessControl(dSecurity);
        }
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Directory Access rights?

#2 poodledoodle  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 26-October 12

Re: Directory Access rights?

Posted 27 October 2012 - 07:41 PM

mayb duh //
Was This Post Helpful? 1
  • +
  • -

#3 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Directory Access rights?

Posted 28 October 2012 - 01:45 AM

View Postpoodledoodle, on 27 October 2012 - 07:41 PM, said:

mayb duh //

explain pls..
Was This Post Helpful? 0
  • +
  • -

#4 DimitriV  Icon User is online

  • They don't think it be like it is, but it do
  • member icon

Reputation: 563
  • View blog
  • Posts: 2,693
  • Joined: 24-July 11

Re: Directory Access rights?

Posted 28 October 2012 - 01:50 AM

View PostDocNet, on 28 October 2012 - 06:45 PM, said:

View Postpoodledoodle, on 27 October 2012 - 07:41 PM, said:

mayb duh //

explain pls..

Last time I checked, the file system doesn't use forward-slashes, let alone two. poodledoodle meant that the forward slashes are the issue. You should be saying:
C:\
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,227
  • Joined: 23-August 08

Re: Directory Access rights?

Posted 28 October 2012 - 03:07 AM

Not quite, you should be using

ListDirectory(treeView1,"C:\\");


because backslash in a string means "the next character needs to be treated specially" (it's called an escape sequence), allowing you to use sequences like \n to mean a new line and \t to mean a TAB.

Alternatively you can prefix the string with @, which turns off the escape sequence functionality.


ListDirectory(treeView1,@"C:\");


Reading material
Was This Post Helpful? 0
  • +
  • -

#6 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Directory Access rights?

Posted 28 October 2012 - 03:32 AM

View PostJackOfAllTrades, on 28 October 2012 - 03:07 AM, said:

Not quite, you should be using

ListDirectory(treeView1,"C:\\");


because backslash in a string means "the next character needs to be treated specially" (it's called an escape sequence), allowing you to use sequences like \n to mean a new line and \t to mean a TAB.

Alternatively you can prefix the string with @, which turns off the escape sequence functionality.


ListDirectory(treeView1,@"C:\");


Reading material


Nope, i still have same error...

When I delete security function i have this error:
Posted Image

When i put security function i have this error:
Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5958
  • View blog
  • Posts: 23,227
  • Joined: 23-August 08

Re: Directory Access rights?

Posted 28 October 2012 - 04:58 PM

Screenshot 1: Catch the error and ignore the file causing the error.

Screenshot 2: You are passing the first argument to the FileSystemAccessRule constructor as a string. According to the documentation it's a System.Security.Principal.IdentityReference object. Not sure that's the same thing, but if it compiles, maybe it is.

/shrugs, wipes hands on pants
Was This Post Helpful? 0
  • +
  • -

#8 DocNet  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 74
  • Joined: 11-October 12

Re: Directory Access rights?

Posted 29 October 2012 - 05:46 AM

Thank you guys i solved this problem,
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1