Copying a file to a network share

F

Frank Rizzo

Hello, I've asked this question in the languages.vb group, but got no
response, so I'll try my luck here.

My app needs to copy a file from a local folder to a network share. The
network share is accessible via a domain ID, which has all possible
folder rights assigned to it. The domain ID also has full rights to the
local folder. I am using the impersonation code from here

http://msdn.microsoft.com/library/d...ImpersonationContextClassTopic.asp?frame=true

The code seems to properly impersonate (as evidenced by debug
statements), however, when I do this:

string sSource = @"c:\temp\ObjectList.zip";
string sTarget = @"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip"

Try
File.Copy(sSource, sTarget)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try

It fails with "System.Io.FileNotFoundException: Could not find the
file..." I know for sure that the file is there. Do I first perhaps
have to log on to the share? If so, are there any examples out there?
What am I missing in general?

Thank you
 
G

Guest

Hi

It works alright for me in C#.

I think the pblm is that the line:
string sTarget = @"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip"

is referencing a file share. Try using a UNC name and you should be fine.

Cheers
Shane Sukul
MCSD MCAD

"Gravitation is not responsible for people falling in love" - Albert Eistein
 
F

Frank Rizzo

Shailen said:
Hi

It works alright for me in C#.

I think the pblm is that the line:




is referencing a file share. Try using a UNC name and you should be fine.
What do you mean. The path is in UNC format?
 
G

GG

Not sure if it helps but
@"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip"
may be should be
@"\\nasrv88c3\modeldev$\modeldev\apps\ObjectList.zip"
There are \\ObjectList.zip instead of 1
 
F

Frank Rizzo

GG said:
Not sure if it helps but
@"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip"
may be should be
@"\\nasrv88c3\modeldev$\modeldev\apps\ObjectList.zip"
There are \\ObjectList.zip instead of 1
Oh, i am sorry, that was just a typo. There is a single slash, not a
double one.
 
G

Guest

By UNC I mean a fully-qualified name to the network folder. For example,

in the line:

@"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip",

"modeldev$" is a share name.

You will have to use the fully qualified name of the
destination folder. For example:
@"\\nasrv88c3\modeldev\myModule\modeldev\apps\\ObjectList.zip",

HTH

Regards
Shane Sukul
MCSD MCAD
 
F

Frank Rizzo

Shailen said:
By UNC I mean a fully-qualified name to the network folder. For example,

in the line:

@"\\nasrv88c3\modeldev$\modeldev\apps\\ObjectList.zip",

"modeldev$" is a share name.

You will have to use the fully qualified name of the
destination folder. For example:
@"\\nasrv88c3\modeldev\myModule\modeldev\apps\\ObjectList.zip",

The point is that I don't know what the fully qualified name is. How
can I copy the file in that case?
 
G

Guest

Ok, in the case where you have to map a shared drive using different user
credentails(if necessary), do the following:

sTarget = "\\some server\some share$\somefile.txt"
source = "c:\temp\somefile.txt"
try
{

File.Copy(sSource, sTarget);

}
catch (Exception ex )
{
try
{
// Map a drive
System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\net.exe",
@"use J: \\some server\some share$ password /user:someuser");

// Delay to allow the changs to take effect
for (int i =0;i<10000;i++)

File.Copy(sSource, sTarget);

}
catch (Exception ex2)
{
MessageBox.Show(ex2.ToString());
}
finally
{
// Delete the mapped drive
System.Diagnostics.Process.Start(@"C:\WINDOWS\system32\net.exe",
"use j: /delete");
}
}

Regards
Shane Sukul
MCSD MCAD
 

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