Copy file on a network shared drive

Viewed 92728

I have a network shared drive ("\serveur\folder") on which I would like to copy file. I can write on the drive with a specific user ("user"/"pass"). How can I access the shared drived with write privilege using C#?

4 Answers

More simple and modern approach. Works for me in an enterprise network.

 try
{
    bool validLogin = false;
    using (PrincipalContext tempcontext = new PrincipalContext(ContextType.Domain, "domain.company.com", null, ContextOptions.Negotiate))
    {
        try
        {
            validLogin = tempcontext.ValidateCredentials("USERNAME", "PASSWORD", ContextOptions.Negotiate);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
    if (validLogin)
    {
        File.Copy(@"C:\folder\filename.txt", @"\\domain\folder\filename.txt", true);
        return true;
    }
    else
    {
        MessageBox.Show("Username or Password is incorrect...", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        return false;
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
    return false;
}
Related