Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 378047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:45:19+00:00 2026-05-12T14:45:19+00:00

I am writing an ASP.NET (C#) application to create users for my domain. It

  • 0

I am writing an ASP.NET (C#) application to create users for my domain. It also has to create folders and shares on a separate file server. I have so far been able to accomplish my task using

  • System.IO.Directory.CreateDirectory to create the folders,
  • a (“WinNT://fileserver/lanmanserver”) DirectoryEntry to create the shares.

Unfortunately, my ASP.NET application has to run with impersonation on to create the folder. I don’t like that. I would like to know if there is a way to create a folder on the file server using a DirectoryEntry object since i can pass the needed credentials to its constructor. Or, alternatively, is there a way to pass credentials to Directory.CreateDirectory?

Thanks in advance.
Here is the current code, just in case

strPath = "\\myServer\D$\newDir";
Directory.CreateDirectory(strPath);

using (DirectoryEntry deFS = new DirectoryEntry("WinNT://myServer/lanmanserver"))
{
    using (DirectoryEntry deSH = deFS.Children.Add("newDir$", "fileshare"))
    {  
       deSH.Properties["path"].Value = "D:\\newDir";
       deSH.Properties["description"].Value = "My Stackoverflow sample share";
       deSH.CommitChanges();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T14:45:20+00:00Added an answer on May 12, 2026 at 2:45 pm

    I don’t believe you should be using DirectoryObject for that purpose, it wasn’t made for such an access. But here’s a trick you could be using to make impersonation easier. Create an impersonator class, which would implement IDisposable, something like this:

    public class Impersonator : IDisposable
    {
        public Impersonator(userid, password) 
        {
            ... LogonUserEx();
            ... DuplicateToken();
            ... Impersonate();
        }
        public void Dispose()
        {
            ... RevertToSelf();
        }
    }
    

    then you would be able to do this:

    using(new Impersonator("myaccount", "password"))
    {
         ... do stuff that requires impersonation
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an ASP.NET MVC application and trying to use a RESX file to
I am writing an ASP.Net MVC application. I have two entities - patients and
I'm writing this question from the standpoint of an ASP.NET application. However I realize
I'm writing an ASP.NET 3.5 web application. There is a rather complex input form
I'm currently writing a C#-class in my ASP.NET (3.5) application to handle all database-queries.
I'm writing a new asp.net mvc application and I've been toying with the idea
I'm writing an ASP.Net MVC application using Jquery to post some data to my
I'm trying to write a log file from an ASP.NET application under IIS7, but
I am writing an ASP.NET application that initializes some contextual data based on stuff
Totally stupid question, but I'm writing my first ASP.NET C# web forms application and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.