473,666 Members | 2,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How can I write to a shared drive on another server

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?
--

Ruepen
Nov 17 '05 #1
3 2248
A small code example would be appreciated. I have tried using this....(which
I found on this forum)

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

public static void Main(string[] args)
{
IntPtr admin_token;
string password = ConfigurationSe ttings.AppSetti ngs["NetDriveAccoun tPwd"];

WindowsIdentity wid_current = WindowsIdentity .GetCurrent();

//WindowsIdentity wid_admin = null;
//WindowsImperson ationContext wic = null;

try
{
Console.WriteLi ne("Copying file...");
if (LogonUser("use rName", "192..168.1 .1", password, 9, 0, out
admin_token) != 0)
{
ImpersonateLogg edOnUser(admin_ token);
Console.WriteLi ne(WindowsIdent ity.GetCurrent( ).Name);
//Console.WriteLi ne(admin_token. ToString());
//wid_admin = new WindowsIdentity (admin_token);
//wic = wid_admin.Imper sonate();
System.IO.File. Copy("C:\\email Output.txt",
"\\\\192.168.1. 1\\FolderName", true);
Console.WriteLi ne("Copy succeeded");
}
else Console.WriteLi ne("Copy Failed");
}
catch (System.Excepti on se)
{
int ret = Marshal.GetLast Win32Error();
Console.WriteLi ne(ret.ToString (), "Error code: " + ret.ToString()) ;
Console.WriteLi ne(se.Message);
}
finally
{
RevertToSelf();
//if (wic != null) wic.Undo();
}
//Console.ReadLin e();
}
--
But I kept on getting error "Access to the path \\\\192.168.1.1 \\FolderName
is denied"

Any help would be appreciated.
Ruepen
"Ruepen" wrote:
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?
--

Ruepen

Nov 17 '05 #2


"Ruepen" <Ru****@discuss ions.microsoft. com> wrote in message
news:1D******** *************** ***********@mic rosoft.com...
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?
--

Ruepen


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

Willy.
Nov 17 '05 #3
Are you sure that "userName" is a valid user on the remote server?
if (LogonUser("use rName", "192..168.1 .1", password, 9, 0, out
admin_token) != 0)
I hope also that this 192.. is not a typo!

Willy.
"Ruepen" <Ru****@discuss ions.microsoft. com> wrote in message
news:5F******** *************** ***********@mic rosoft.com...A small code example would be appreciated. I have tried using
this....(whi ch
I found on this forum)

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

public static void Main(string[] args)
{
IntPtr admin_token;
string password = ConfigurationSe ttings.AppSetti ngs["NetDriveAccoun tPwd"];

WindowsIdentity wid_current = WindowsIdentity .GetCurrent();

//WindowsIdentity wid_admin = null;
//WindowsImperson ationContext wic = null;

try
{
Console.WriteLi ne("Copying file...");
if (LogonUser("use rName", "192..168.1 .1", password, 9, 0, out
admin_token) != 0)
{
ImpersonateLogg edOnUser(admin_ token);
Console.WriteLi ne(WindowsIdent ity.GetCurrent( ).Name);
//Console.WriteLi ne(admin_token. ToString());
//wid_admin = new WindowsIdentity (admin_token);
//wic = wid_admin.Imper sonate();
System.IO.File. Copy("C:\\email Output.txt",
"\\\\192.168.1. 1\\FolderName", true);
Console.WriteLi ne("Copy succeeded");
}
else Console.WriteLi ne("Copy Failed");
}
catch (System.Excepti on se)
{
int ret = Marshal.GetLast Win32Error();
Console.WriteLi ne(ret.ToString (), "Error code: " + ret.ToString()) ;
Console.WriteLi ne(se.Message);
}
finally
{
RevertToSelf();
//if (wic != null) wic.Undo();
}
//Console.ReadLin e();
}
--
But I kept on getting error "Access to the path
\\\\192.168.1.1 \\FolderName
is denied"

Any help would be appreciated.
Ruepen
"Ruepen" wrote:
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?
--

Ruepen

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
2577
by: Rodrigue | last post by:
I as a developer have an application that I can run from my box or from a shared drive, but I cannot get anybody else to run it. They do have the same version of the framework on their boxes, yet they are unable to run the application. What do I need to do. The error is not a security exception
2
3619
by: Daniel | last post by:
when i use stream writer to write to a shared folder, and the network card of that remote computer is disabled in the middle of the write then reenabled later. every time i try to write to that same path on the network drive it says "The process cannot access the file "\\foo\bar.txt" because it is being used by another process. its not until i restart my exe that it can access it. I tried catching the error and closing the file when the...
0
3033
by: Peter Horwood | last post by:
I am having trouble writing an XML file out on my localhost development machine. I get: Server Error in '/ProblemSolved' Application. ----------------------------------------------------------- --------------------- Access to the path "c:\inetpub\wwwroot\ProblemSolved\XMLData\Products.xml
11
1526
by: Brett | last post by:
I have an assembly on a shared LAN drive. On my developer machine, I give that assembly full trust from the .NET wizard. It works fine. I go to a user machine on the LAN, map to the shared drive, give full trust on that machine but the assembly doesn't fully work. No errors. The assembly post data from MS Access to a website. The posting part doesn't work. The firewall was turned off and still nothing. Any ideas? Thanks,
4
3709
by: Glenn M | last post by:
I have a shared XML file on a server . i also have one xslt file that performs a simple transform on in to view the data. now i want to have another page that lets users modify the shared xml file via some editable controls such as text boxes , option boxes etc. how can i implment this , should i use another xslt file with <INPUT> controls . if so how can i save the result back using the asp.net
0
1658
by: Seth | last post by:
For some reason my service works fine except that it will create the file in my c drive, but will not write to the file. Sorry if this is a duplicate post, i have found some that ask the same question - but noone seems to answer. Do i need to do anything specific to write to a text file from a windows service, much appreciated.
3
1478
by: c_shah | last post by:
I am having a .NET Application. Install the application on a Share drive on the network. Any user WHO's machine is connected to network can access the application by clicking on short cut on his desktop. Now the application runs on the local machine, or it runs on the Share Drive?
2
1096
by: Rich | last post by:
Hello, I have a winform app that end users in my company use for various operations - mainly searching stuff. It is a VB2005 app. People enter parameters and want to be able to recall what parameters they have entered. Right now I have them enter their params into a dataTable/datagridview and I have the dataTable write its contents to the local workstation in xml format when the user is finished. But if a user is not at their...
0
2103
by: =?Utf-8?B?SmltSGVhdmV5?= | last post by:
I developed some web pages and stored those pages on a "removable drive". All was right with the world. I then took the "removable drive" and moved it to another machine but mapped that drive with the same drive letter on the "home machine". Now when I bring up those web pages within VS 2005, I get this error which refers me to KB article 810886 which tells me that I need to increase the values for "MaxCmds" & "MaxMpxCt". I increased...
0
8362
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8878
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8785
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8560
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7389
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5671
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.