How to move a file that has no file extension? C#

后端 未结 3 1881
你的背包
你的背包 2021-01-23 04:52
if (File.Exists(@\"C:\\\\Users\" + Environment.UserName + \"\\\\Desktop\\\\test\"))
{                                                                /\\
                         


        
3条回答
  •  猫巷女王i
    2021-01-23 05:43

    There's nothing special about extensionless files. Your code is broken because you use string concatenation to build a path and you're mixing verbatim and regular string literal syntax. Use the proper framework method for this: Path.Combine().

    string fullPath = Path.Combine(@"C:\Users", Environment.UserName, @"Desktop\test");
    
    if(File.Exists(fullPath))
    {
    
    }
    

    You also should use the proper framework method to get the desktop path for the current user, see How to get a path to the desktop for current user in C#?:

    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    
    string fullPath = Path.Combine(desktopPath, "test");
    

    Then you can call File.Move() to rename the file, see Rename a file in C#:

    if(File.Exists(fullPath))
    {
        string newPath = fullPath + ".txt";     
        File.Move(fullPath, newPath);
    }
    

提交回复
热议问题