Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I get access denied error when I acces the folder which is netwrok,
However the locally accessing within the system works fine lists all the files and folders

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;


namespace IBISApplication
{
    class Program
    {
        static void Main(string[] args)
        {

          
     
            DirSearch("\\\\10.143.14.140\\D$\\nrtfiles");
            //DirSearch(@"C:\Users\U6037395\Documents\TRWork");

            Console.ReadKey();


        }

        static void DirSearch(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    DirSearch(d);
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}


What I have tried:

I tried giving the permission has all for the network folder
Added my User name in the folder sharing permission


I am getting access denied error.
Posted
Updated 21-Jul-16 0:47am
v2

1 solution

You're accessing the machine using the default admin share for the drive (d$), it's unlikely you amended the permissions for that as you need to be admin to access it. Access the folder via a proper network share so you can control the rights, don't use "d$" - that's a "hack" share.

\\computername\sharedfoldername

to remove the sharename from the output (call the function as normal)

C#
static void DirSearch(string dir, string rootDir = null)
{
    if (rootDir == null)
    {
        rootDir = dir;
    }
    try
    {
        foreach (string f in Directory.GetFiles(dir))
        {
            string filename = f.Substring(rootDir.Length);
            Console.WriteLine(filename);
        }
        foreach (string d in Directory.GetDirectories(dir))
        {
            Console.WriteLine(d);
            DirSearch(d, rootDir);
        }

    }
    catch (System.Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}
 
Share this answer
 
v3
Comments
ShaHam11 21-Jul-16 7:11am    
thanks for the soultion I removed the d$ it worked. However it gives the full path name I am intrested only in the folder name and file name not the whole path any suggestions on this?
F-ES Sitecore 21-Jul-16 8:10am    
You'll just need to remove the name of the computer from the full path whenever you show it on screen, I'll update the solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900