Link to home
Create AccountLog in
Avatar of exalkonium
exalkoniumFlag for United States of America

asked on

C# Manipulate files and folders on one server from another


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();

Open in new window

Avatar of iHadi
Flag of Syrian Arab Republic image

I think a more mind peace solution is to use webservices.

Write a webservice method that takes a byte array as a parameter and saves it to disk.

Install this webservice on the server. Use it from the client to store files.

This approach will override security problems, firewall/proxy limitations and can be used from anywhere on the network without the hassle of file sharing and unc
Avatar of vora_bhaumik
Flag of India image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account