Using File.Copy() Correctly

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!




Answer this question

Using File.Copy() Correctly

  • Raffaele Rialdi

    Using the code you had done, I get an error of "StandardError has not been redirected." This happens when the code

    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.



  • aliasx

    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");



  • Jehan Badshah

    I guess, in the end, there was nothing we can do to fix this problem. I believe that there is something that both file.move and file.copy do not like about a temporary drive set up. So, I am going to attempt to use FileWebRequest code for downloading/uploading files to/from computers, and the System.Diagnostics.Process code that people helped with to get the names of files in a folder on someone's computer. Thanks to everyone for their help! It was much appreciated!

  • Sondre - MSFT Regional Director

    You want shell execute set to false. To clarify, the following is fine for me:


    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);
    }
    }

  • Annihil8

    Sorry, I was copying some old code of mine and forgot to change one of the variable names - it should be 'process'. I'd had to write it twice because the msdn forum lost my post when previewing (grr) so became careless.

    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

  • WizMan

    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!



  • Thunder2002

    Your problem is probably either a race condition because your drive mapping is occurring asynchronously in a different process so has not completed by the time your file copy is attempted and/or your drive mapping fails but you unaware of it.

    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);
    }
    }


  • Rajith

    You code is working perfectly, but (I hate to say "but") when I get to file.copy, I get a System.IO.DirectoryNotFoundException claiming "Could not find a part of the path 'z:\New Text Document.txt'. Maybe should I try doing a file.move There is no reason why it should not like that path... I know this is the correct path (I have checked my z drive when it is available that that is what it shows: z:\NameOfFile). Sorry this is such a pain! You would think something this simple shouldn't be so hard.

  • A.Carter

    Yes.

  • RajMohapatra

    You want to call start on the process object that has been created and not call use the static System.Diagnostics.Process.Start.

    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);
    }
    }




  • StairCounter

    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!



  • meho

    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.



  • priorityversion

    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).



  • Using File.Copy() Correctly