I have been trying to use File.Copy to copy a file from a z drive that I made to a location in my C drive. The problem is I will get the error of "Could not find a part of the path 'z:\New Text Document.txt'" and I do not know what that means. This is the line I have been using, but let me know if I am wrong:
File
.Copy("z:\New Text Document.txt", "C:\AAAWorkspace\FTPProgramWinApp2\bin\Debug\New Text Document.txt");I am 100% sure that both of these places (the z and c drives) exist at the time the code is executed. If it matters, I made the z drive via:
sdps = System.Diagnostics.Process.Start("net", @"use z: \\" + downloadLocation + " " + pswd + @" /user:" + user);
... where downloadLocation is the IPaddress\folder, pswd is the password, and user is the computerName\userName needed.
Ultimately, I would like to be able to either make a copy of the file to the C drive, then delete it from the Z drive OR just move the file from the Z drive to the C drive. I have not been able to get the File.move() to work either, so that is why I am attempting File.Copy().
Thanks ahead of time! If I am in the wrong forum, let me know and either move this post (if you can) or I will. Thanks!

Using File.Copy() Correctly
rajshekar
You really need to check the return code and messages.
Is the mapping visible in "My Computer" if you don't try and delete it
If you do the net call on the cmd line and just call the Copy in code, does it work
luca82
By using Process slightly differently, you can cover both possibilities. First wait for the command completion like this:
System.Diagnostics.Process process = new System.Diagnostics.Process.Process();
process.StartInfo.... // Set what you need to here:
dotExe.StartInfo.UseShellExecute = true;
dotExe.StartInfo.RedirectStandardOutput = true;
dotExe.StartInfo.RedirectStandardError = true;
dotExe.Start();
dotExe.WaitForExit();
You can then examine the success from your mapping command by:
if (process.ExitCode != 0)
{
StreamReader stdErr = process.StandardError;
string result = stdErr.ReadToEnd();
if (result.Length > 0)
{
log.Error(result);
}
}
else
{
StreamReader stdOut = process.StandardOutput;
string result = stdOut.ReadToEnd();
if (result.Length > 0)
{
log.Info(result);
}
}
AlexBB
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.FileName = "Net.exe";
process.StartInfo.Arguments = "use zig zag to the onion bag";
process.Start();
process.WaitForExit();
if (process.ExitCode != 0)
{
System.IO.StreamReader stdErr = process.StandardError;
string result = stdErr.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
else
{
System.IO.StreamReader stdOut = process.StandardOutput;
string result = stdOut.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
Kent Boogaart
You instead need to replace the placeholder I wrote:
"zig zag to the onion bag"
with your actual arguments:
@"use z: \\" + downloadLocation + " " + pswd + " /user:" + user
and then call
sdps.Start();
e.g.
string downloadLocation = "bah";
string user = "bah";
string pswd = "bah";
System.Diagnostics.Process sdps; //sdps made here because if an error is thrown later on, we
//need to be able to do " "net", @"use z: /delete" " so here is
//where it needs to be originally made
sdps = new System.Diagnostics.Process();
sdps.StartInfo.UseShellExecute = false;
sdps.StartInfo.RedirectStandardError = true;
sdps.StartInfo.RedirectStandardOutput = true;
sdps.StartInfo.FileName = "Net.exe";
sdps.StartInfo.Arguments = @"use z: \\" + downloadLocation + " " + pswd + " /user:" + user;
sdps.Start();
sdps.WaitForExit();
if (sdps.ExitCode != 0)
{
System.IO.StreamReader stdErr = sdps.StandardError;
string result = stdErr.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
else
{
System.IO.StreamReader stdOut = sdps.StandardOutput;
string result = stdOut.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
Pooja Katiyar
I have tried:
sdps = System.Diagnostics.Process.Start("net", @"use z: \\" + downloadLocation + " " + pswd + @" /user:" + user);
sdps.WaitForExit();
string downloadFileTo = downloadLocation + @"\" + filename;
File.Copy(downloadUrl, downloadFileTo);
sdps = System.Diagnostics.Process.Start("net", @"use z: /delete");
where sdps = System.Diagnostics.Process but get the same problem as before. For your code above Duncan, where did you get dotExe from Your code makes sense but that is the only thing I do not get (dotExe).
Thanks!
GSReddy
Wayne Sepega
if (sdps.ExitCode != 0)
{
StreamReader stdErr = sdps.StandardError;
string result = stdErr.ReadToEnd();
if (result.Length > 0)
{
ErrorsHappened.Items.Add(result);
}
}
is trying to be executed. It gets to the StreamReader line and throws an InvalidOperationException error with the message from above. So, am I a hopless cause
Sorry, I cannot seem to figure this out. C# in general is still pretty new to me and this code more specifically is very new.
AshishGupta
Adam Juza
If I have two strings coming in -- string1 for z:\... and string2 for C:\... will I get the same results that you say if I do:
File.Copy(string1, string2);
Ultimately I would like to generalize it so I can use it pending on user input. Thanks!
IgorV
Ok, I used 99% of your code but I am still getting it throwing an error when it tries the line "System.IO.StreamReader stdErr = sdps.StandardError;". Below is how I used the code... I am not sure why my program does not like it.
public
Boolean Download(string downloadUrl, string user, string pswd, string downloadLocation, Boolean multfiles, string filename){
Boolean returnAns = false;
System.Diagnostics.Process sdps; //sdps made here because if an error is thrown later on, we
//need to be able to do " "net", @"use z: /delete" " so here is
//where it needs to be originally made
if (multfiles == false) //don't mind this if statement
{
//write to email transfer number
transfer++;
errors = errors + "\r\n\r\nTransfer #" + transfer;
}
try
{
sdps = new System.Diagnostics.Process();
sdps.StartInfo.UseShellExecute = false;
sdps.StartInfo.RedirectStandardError = true;
sdps.StartInfo.RedirectStandardOutput = true;
sdps.StartInfo.FileName = "Net.exe";
sdps.StartInfo.Arguments = "use zig zag to the onion bag";
sdps = System.Diagnostics.Process.Start("net", @"use z: \\" + downloadLocation + " " + pswd
+ " /user:" + user);
sdps.WaitForExit(); //so far so good up to and including this line
if (sdps.ExitCode != 0)
{
System.IO.StreamReader stdErr = sdps.StandardError; //here is where an error is thrown
string result = stdErr.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
else
{
System.IO.StreamReader stdOut = sdps.StandardOutput;
string result = stdOut.ReadToEnd();
if (result.Length > 0)
{
MessageBox.Show(result);
}
}
string downloadFileTo = downloadLocation + @"\" + filename;
File.Copy(downloadUrl, downloadFileTo); //if I comment out the if/else statements above, this line
//will fail with the original error message I have been
//getting
sdps = System.Diagnostics.Process.Start("net", @"use z: /delete");
...
}
...
}
What could be going wrong here See my comments by code for what happens where if you have not already. The "..." just mean there is more code that you don't have to worry about because it should not affect the code listed here (like the catches for the various errors, some writing to text boxes etc).
Juvefan
That did not work. If it matters, I am getting a System.IO.DirectoryNotFoundException (the same error as before) but I am doing exactly what you said:
sdps = System.Diagnostics.
Process.Start("net", @"use z: \\" + downloadLocation + " " + pswd + @" /user:" + user); string downloadFileTo = downloadLocation + @"\" + filename; File.Copy(downloadUrl, downloadFileTo);downloadLocation is the "C:\..." and file name is the file name. I put these together to get the correct URL. downloadUrl is the "z:\..." So I am not sure what is going wrong. I made sure that sdps had enough time to load.
1Dave
Marek Istvanek
File.Copy("z:\New Text Document.txt", "C:\AAAWorkspace\FTPProgramWinApp2\bin\Debug\New Text Document.txt");
That won;t work. What you'd need is either:
File.Copy(@"z:\New Text Document.txt", @"C:\AAAWorkspace\FTPProgramWinApp2\bin\Debug\New Text Document.txt");
or
File.Copy("z:\\New Text Document.txt", "C:\\AAAWorkspace\\FTPProgramWinApp2\\bin\\Debug\\New Text Document.txt");