Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.Copy, over a network, only words in Debug

Status
Not open for further replies.

tgreer

Programmer
Oct 4, 2002
1,781
US
I'm using File.Copy() to copy a file from the local filesystem to a mapped network drive. (I'd like to use UNC, but couldn't get it to work. I suspect that both problems are the same.)

This works when I step through the code. When I simply run, I get an exception, "could not find a part of the path".

I strongly suspect permissions, though the share is set to Full Control for everyone. I know that .NET has it's own issues with "trusted network shares".

This is a C# console application. What's the answer? Is there something I can put in the app.config file to trust the network share? Would that allow me to use UNC? Do I need to use impersonation?



Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
...thinking this through a bit more. The Console application in question, in production, is spawned by a Windows Service I've written. I vaguely remember something about "act as part of the OS" and "interact with the desktop" permissions. Which user account would need those permissions, in this scenario?

If that's even the issue.

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Although its VB.Net it should be very easy to convert if it is of use:

thread796-1039493


Hope this helps.

 
Read the whole thread... it didn't really address the specific issue. Here's some code:

Code:
if (!Directory.Exists(sack_destination + tape_name))
{
Directory.CreateDirectory(sack_destination + tape_name);
}
string source_file = pdf_folder + @"\sack_final.pdf";
string dest_file = sack_destination + tape_name + @"\" + run_name + "_" + tape_name + "_" + sack_name + ".pdf";

File.Copy(source_file, dest_file ,true);

The "sack_destination" is the network share. I retrieve it from the app.config file, not that it matters.

Run in debug mode, this works fine. When spawned via a service, there are errors both on the "CreateDirectory" and the File.Copy. The issue is it won't even "look at" the network share.

This has to be an issue with the permissions of the service, but running the service under a user account doesn't fix the issue.

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Not having a network at home I wasn't able to test anything, and remembering that Tim had eventually come up with a solution after much to-ing and fro-ing, I thought it was worth you having a look at. Sorry it wasn't of more use.
 
I think what you need to do is setup a NETRESOURCE.

The first thing you need to do is setup a NETRESOURCE struct:

Code:
		// This must be used if NETRESOURCE is defined as a struct
		[DllImport("mpr.dll")]
		public static extern int WNetAddConnection2(NETRESOURCE netResource,
			string password, string username, int flags);
	}

	// Use this when the unmanaged API expects the structure passed by-value, or
	// or if you want to pass it by-reference as a pointer to a structure
	public struct NETRESOURCE
	{
		public int dwScope;
		public int dwType;
		public int dwDisplayType;
		public int dwUsage;
		public string LocalName;
		public string RemoteName;
		public string Comment;
		public string Provider;

...then setup your NETRESOURCE...
Code:
		private void SetupNetworkConnection()
			//Setup network connection to retrieve Bitmap image for questions
		{
			NETRESOURCE myNetResource = new NETRESOURCE();
			myNetResource.dwScope = 2;
			myNetResource.dwType = 1 ;
			myNetResource.dwDisplayType = 3;
			myNetResource.dwUsage = 1;
			myNetResource.RemoteName = @"your UNC path";
			myNetResource.Provider = null;
            //A valid username and password for the network resource must be supplied
			int ret = WNetAddConnection2(myNetResource,"your password","your username",0);
		}

...and then call the setup method in your class constructor. I had some problems retrieving bitmap files from a network resource and this worked like a charm.

HTH,
--Brad
 
Switchting to UNC path (since drive mappings are assinged at logon, for that session, services cannot use network USER drive mappings), and running under a particular user account rather than LocalSystem, fixed the problem. Thanks for the responses.

Thomas D. Greer

Providing PostScript & PDF
Training, Development & Consulting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top