Copy file from one directory to another

后端 未结 5 473
执笔经年
执笔经年 2021-01-28 13:11

I am pretty new to C# and I am trying to get my program to copy a file from one location to another. The method I have is as below;

    private void CopyInstallF         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-28 13:47

    This is because in C# (and C++ and C and some other languages) string can contain special characters. Those characters are followed by '\'. So for example string:

    "\n"
    

    Will not show you \n This is special character called - new line. So, when you create path like that:

    "C:\Dir\file.txt"
    

    C# expects that there are two special characters: \D and \f. But there is no special characters like that. Thus the error.

    To put character '\' into string you have to double it, so:

    "\\n"
    

    would output \n

    The same is with paths: "C:\Dir\file.txt"

    C# has an alternative. You can have single '\' in path, but such a string must be followed by at sign (@):

    string properPath = @"C:\dir\file.txt";
    string properPath2 = "C:\\dir\\file.txt";
    string error = "C:\dir\file.txt"
    

提交回复
热议问题