0

I am quite confused as to why my code isn't working. I wanted to transfer a textfile that I have into another folder. Here is my code:

private void transferButton_Click(object sender, EventArgs e)
    {
        string acct = @"C:\\Users\\Accounting\\TicketQueue\\";
        string reg = @"C:\\Users\\Registrar\\TicketQueue\\";

        if (office == "Registrar")  
        {
            File.Move(reg, acct);
        } 
        else {
            File.Move(acct, reg);
        }

        cleanUp();
    }

The office variable is determined beforehand. (Registrar or Accounting)

The cleanup() method is used to clear the entire form and prompt a message that successfully transfers the file.

Everytime I click the button an error displays saying:

Additional information: Could not find file 'C:\Users\Accounting\TicketQueue\'.

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
Async
  • 17
  • 1
  • 6
  • 1
    the paths you have specified relate to Directories not Files. You need to include a filename in the acct and reg strings like "...\\file.txt" – CodexNZ Aug 16 '17 at 01:15

1 Answers1

1

You have not actually specified a file name, just a folder location. Do you know the name of the file, or are you trying to transfer the entire folder's contents? Code would need to be something like:

string acct = @"C:\\Users\\Accounting\\TicketQueue\\from.txt";
string reg = @"C:\\Users\\Registrar\\TicketQueue\\to.txt";
see sharper
  • 11,505
  • 8
  • 46
  • 65
  • Thanks. This cleared the error but unfortunately, another error occurred. This time it says "being used by another process" so it won't move. – Async Aug 16 '17 at 01:39
  • What else is accessing the file? – see sharper Aug 16 '17 at 01:41
  • I don't know either. I think that it is because of my StreamReader because each lines inside the Textfile is being displayed in different textboxes in my windows form. – Async Aug 16 '17 at 01:58
  • Yeah, sounds like you are holding an open file handle. You probably need to read the file to some buffer, release the file handle, write back when required. – see sharper Aug 16 '17 at 02:04
  • Can you give me a sample or concept of how I can do that? Sorry I'm a beginner! – Async Aug 16 '17 at 02:43
  • 1
    Basically, when you read the file, make sure you immediately call StreamReader.Close() - or wrap the read in a 'using' clause. Read the file contents into a string - or perhaps an array of strings if you want separate text boxes per line. When the user clicks 'transfer' or whatever, then call your method. This assumes that you are the one keeping the open file handle and not some other process. – see sharper Aug 16 '17 at 02:51
  • Agree, good idea to wrap in a 'using' statement. Then you can be quite sure your StreamReader will be closed even in the case of an exception. The '[using](https://stackoverflow.com/questions/518352/does-dispose-still-get-called-when-exception-is-thrown-inside-of-a-using-stateme)' will `try` what's in its scope, then close the StreamReader in a `finally` block. – Poosh Aug 16 '17 at 04:03
  • Oh so that's it. Can I do it manually like call the close method instead of the `using` function? Because I'm currently using the `using` statement and it still causes the same error. Although I haven't used the `finally` block. – Async Aug 17 '17 at 00:17