How can I write to a shared drive on another server

G

Guest

I need to write to a file on a shared drive on another server using user
credentials.
I can't have this permanently mapped either due to application server
restrictions that my app is running on.
Can this be done using C# classes only?
 
G

Guest

A small code example would be appreciated. I have tried using this....(which
I found on this forum)

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String
lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out
IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
handle to token for logged-on user
[DllImport("advapi32.DLL")] public static extern bool RevertToSelf();



public static void Main(string[] args)
{
IntPtr admin_token;
string password = ConfigurationSettings.AppSettings["NetDriveAccountPwd"];

WindowsIdentity wid_current = WindowsIdentity.GetCurrent();

//WindowsIdentity wid_admin = null;
//WindowsImpersonationContext wic = null;

try
{
Console.WriteLine("Copying file...");
if (LogonUser("userName", "192..168.1.1", password, 9, 0, out
admin_token) != 0)
{
ImpersonateLoggedOnUser(admin_token);
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
//Console.WriteLine(admin_token.ToString());
//wid_admin = new WindowsIdentity(admin_token);
//wic = wid_admin.Impersonate();
System.IO.File.Copy("C:\\emailOutput.txt",
"\\\\192.168.1.1\\FolderName", true);
Console.WriteLine("Copy succeeded");
}
else Console.WriteLine("Copy Failed");
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine(ret.ToString(), "Error code: " + ret.ToString());
Console.WriteLine(se.Message);
}
finally
{
RevertToSelf();
//if (wic != null) wic.Undo();
}
//Console.ReadLine();
}
--


But I kept on getting error "Access to the path \\\\192.168.1.1\\FolderName
is denied"

Any help would be appreciated.


Ruepen
 
W

Willy Denoyette [MVP]

Ruepen said:
I need to write to a file on a shared drive on another server using user
credentials.
I can't have this permanently mapped either due to application server
restrictions that my app is running on.
Can this be done using C# classes only?

Don't use mapped drives, use UNC path's when accessing remote resources.

Willy.
 
W

Willy Denoyette [MVP]

Are you sure that "userName" is a valid user on the remote server?
if (LogonUser("userName", "192..168.1.1", password, 9, 0, out
admin_token) != 0)

I hope also that this 192.. is not a typo!

Willy.


Ruepen said:
A small code example would be appreciated. I have tried using
this....(which
I found on this forum)

[DllImport("advapi32.dll")]
public static extern int LogonUser(String lpszUsername, String
lpszDomain, String lpszPassword, int dwLogonType, int dwLogonProvider, out
IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //
handle to token for logged-on user
[DllImport("advapi32.DLL")] public static extern bool RevertToSelf();



public static void Main(string[] args)
{
IntPtr admin_token;
string password = ConfigurationSettings.AppSettings["NetDriveAccountPwd"];

WindowsIdentity wid_current = WindowsIdentity.GetCurrent();

//WindowsIdentity wid_admin = null;
//WindowsImpersonationContext wic = null;

try
{
Console.WriteLine("Copying file...");
if (LogonUser("userName", "192..168.1.1", password, 9, 0, out
admin_token) != 0)
{
ImpersonateLoggedOnUser(admin_token);
Console.WriteLine(WindowsIdentity.GetCurrent().Name);
//Console.WriteLine(admin_token.ToString());
//wid_admin = new WindowsIdentity(admin_token);
//wic = wid_admin.Impersonate();
System.IO.File.Copy("C:\\emailOutput.txt",
"\\\\192.168.1.1\\FolderName", true);
Console.WriteLine("Copy succeeded");
}
else Console.WriteLine("Copy Failed");
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
Console.WriteLine(ret.ToString(), "Error code: " + ret.ToString());
Console.WriteLine(se.Message);
}
finally
{
RevertToSelf();
//if (wic != null) wic.Undo();
}
//Console.ReadLine();
}
--


But I kept on getting error "Access to the path
\\\\192.168.1.1\\FolderName
is denied"

Any help would be appreciated.


Ruepen


Ruepen said:
I need to write to a file on a shared drive on another server using user
credentials.
I can't have this permanently mapped either due to application server
restrictions that my app is running on.
Can this be done using C# classes only?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top