Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a network shared drive ("\\serverIP\folder") on which I would want to create text file and add some records. I can access on the drive path with a specific user ("user"/"pass"). How can I access the shared location and create text file using C#?


C#
 string filePath = "\\\\10.1.2.179\\source\\";
                StreamWriter SW1;
FileIOPermission myPerm = new FileIOPermission(FileIOPermissionAccess.AllAccess,FillePath+fileName);
                myPerm.Assert();
                SW1 = File.CreateText(filePath+fileName);



Thanks,
Posted

1 solution

you can impersonate that user for creating the file. check below blog post.
impersonation code C#[^]

I have copied code from that blog post,
C#
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;
 
public class ImpersonatedUser : IDisposable
{
    IntPtr userHandle;
    WindowsImpersonationContext impersonationContext;
 
    public ImpersonatedUser(string user, string domain, string password)
    {
        userHandle = IntPtr.Zero;
        bool loggedOn = LogonUser(
            user,
            domain,
            password,
            LogonType.Interactive,
            LogonProvider.Default,
            out userHandle);
 
        if (!loggedOn)
            throw new Win32Exception(Marshal.GetLastWin32Error());
 
        // Begin impersonating the user
        impersonationContext = WindowsIdentity.Impersonate(userHandle);
    }
 
    public void Dispose()
    {
        if (userHandle != IntPtr.Zero)
        {
            CloseHandle(userHandle);
            userHandle = IntPtr.Zero;
            impersonationContext.Undo();
        }
    } 
 
    [DllImport("advapi32.dll", SetLastError = true)]
    static extern bool LogonUser(
        string lpszUsername,
        string lpszDomain,
        string lpszPassword,
        LogonType dwLogonType,
        LogonProvider dwLogonProvider,
        out IntPtr phToken
        );
 
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern bool CloseHandle(IntPtr hHandle);
 
    enum LogonType : int
    {
        Interactive = 2,
        Network = 3,
        Batch = 4,
        Service = 5,
        NetworkCleartext = 8,
        NewCredentials = 9,
    }
 
    enum LogonProvider : int
    {
        Default = 0,
    }
}


add above class to your project and do as below

C#
using (var impersonation = new ImpersonatedUser(username, domain, password))
{
    string filePath = "\\\\10.1.2.179\\source\\";
    StreamWriter SW1;
    FileIOPermission myPerm = new FileIOPermission(FileIOPermissionAccess.AllAccess,FillePath+fileName);
    myPerm.Assert();
    SW1 = File.CreateText(filePath+fileName);
}

if you want to learn more on Impersonating read below article
A Complete Impersonation Demo in C#.NET[^]
 
Share this answer
 
v2
Comments
Soft009 28-May-14 2:20am    
Thanks a lot,

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