troubleshooting Question

C# Manipulate files and folders on one server from another

Avatar of exalkonium
exalkoniumFlag for United States of America asked on
C#Microsoft IIS Web ServerASP.NET
2 Comments1 Solution382 ViewsLast Modified:

I am trying to programmatically create folders and copy files using C#. The code is sitting on a server in our domain, "app-server". The code uses a UNC path to try and recursively create folders at the moment, but I cannot create one folder as access is denied. I have tried impersonating the network administrator account, but that doesn't seem to do anything. I also tried giving rights of the folder to NETWORK SERVICE. Any advice on how to get this resolved?
sing System;
using System.Collections.Generic;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using CrmSdk;
using CRM;
using System.Security.Principal;
using System.Runtime.InteropServices;

public partial class networkrepository : System.Web.UI.Page
    const int LOGON32_LOGON_INTERACTIVE = 2;
    const int LOGON32_LOGON_NETWORK = 3;
    const int LOGON32_LOGON_BATCH = 4;
    const int LOGON32_LOGON_SERVICE = 5;
    const int LOGON32_LOGON_UNLOCK = 7;
    const int LOGON32_LOGON_NEW_CREDENTIALS = 9;
    const int LOGON32_PROVIDER_DEFAULT = 0;

    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
    [DllImport("advapi32.dll", SetLastError = true)]
    public static extern int ImpersonateLoggedOnUser(IntPtr hToken);

    [DllImport("advapi32.dll", SetLastError = true)]
    static extern int RevertToSelf();

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern int CloseHandle(IntPtr hObject);


    PFS_MSCRMEntities1 db = new PFS_MSCRMEntities1();

    protected void Page_Load(object sender, EventArgs e)
        IntPtr token;
        int result = LogonUser("Administrator", ".", "mypassword", LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, out token);

        IEnumerable<FilteredContact> contacts = (from a in db.FilteredContact
                                                 where a.statecodename == "Active"
                                                 select a);
        string targetPath = @"\\pfsfa-server\pfsdata\Physicians Financial Services\Clients\";

        foreach (FilteredContact contact in contacts)
            string newPath = targetPath + contact.contactid.ToString();

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros