无法复制/在文件名末尾移动空间文件 [英] Can't copy/move files with space at end of file name

查看:116
本文介绍了无法复制/在文件名末尾移动空间文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这真是太疯狂了!我已经用远2.0文件( http://www.farmanager.com/ ,也许你可以使用其他一些文件管理器);它的文件名是'C:\123.txt (是的,与文件路径的末尾空间)

It's really crazy! I have created a file using Far 2.0 (http://www.farmanager.com/, maybe you can use some other file manager); its filename is 'C:\123.txt ' (yes, with space at the end of filepath).

和我。尝试使用C#程序复制或移动文件:

And I'm trying to copy or move this file using a C# program:

File.Copy("C:\\123.txt ", "C:\\456.txt", true);



但它失败,出现找不到文件'C:\123.txt'。例外。但该文件存在。

But it fails with the "Could not find file 'C:\123.txt '." exception. But the file exists!

我试图在Windows API:

I'm trying the Windows API:

[DllImport("kernel32.dll")]
public static extern int MoveFile(string lpExistingFileName, string lpNewFileName);
MoveFile("C:\\123.txt ", "C:\\456.txt",);



但失败了。

But it fails too.

和我想Xcopy实用程序:

And I'm trying the xcopy utility:

C:\>xcopy "C:\123.txt " "C:\456.txt" /Y
File not found - 123.txt
0 File(s) copied

我如何可以以编程方式重命名文件? ?又为什么会发生这种情况。

How can I can rename the file programmatically? And why does this happen?

我的操作系统:Windows 7 64位系统

My OS: Windows 7 x64

推荐答案

你在你的文件名字符在Win32中是非法的。为了规避在Win32路径分析器,你就必须前缀你的文件名\\ \?。例如:

You have a character in your filename that's illegal in Win32. To circumvent the Win32 path parser, you just have to prefix your filename with \\?\. For example:

MoveFile(@"\\?\C:\123.txt ", "C:\\456.txt");

这技术也将让你有路径的最大长度为32K(你只能得到260包括开车在Win32中字母)。

This technique will also allow you to have paths up to 32k in length (you only get 260 including the drive letter in Win32).

这篇关于无法复制/在文件名末尾移动空间文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆