C#新手-无法获取文件。请复制以开始工作
这是一个来自Microsoft的代码示例,文件位置不同,无法正常工作:C#新手-无法获取文件。请复制以开始工作,c#,string,verbatim,C#,String,Verbatim,这是一个来自Microsoft的代码示例,文件位置不同,无法正常工作: string fileName = "test1.txt"; string sourcePath = @"C:\"; string targetPath = @"C:\Test\"; // Use Path class to manipulate file and directory paths. string sourceFile = System.IO.Path.Combine(sourcePath, fil
string fileName = "test1.txt";
string sourcePath = @"C:\";
string targetPath = @"C:\Test\";
// Use Path class to manipulate file and directory paths.
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
它找不到源文件。如果我设置了一个断点,这就是我得到的结果:
args {string[0]} string[]
fileName "test1.txt" string
sourcePath "C:\\" string
targetPath "C:\\Test\\" string
sourceFile "C:\\test1.txt" string
destFile "C:\\Test\\test1.txt" string
因此,即使使用了逐字字符串,它看起来也在加倍反斜杠。(毫无疑问,我有一个C:.格式的test1.txt文件)有什么想法吗?谢谢 双反斜杠是在字符串中表示反斜杠的正常方式。当您使用@时,您是说您不想解释任何转义序列(除其他外,请参阅“文本”下的更多信息) 所以问题就不同了。C:\test1.txt和C:\Test是否存在?您有在targetPath中写入的权限吗 尝试以下操作(根据需要添加异常处理和更多错误检查)
有3种常见的故障模式:
C:\test1.txt
不存在C:\Test\test1.txt
存在,但为只读文件C:\Test
不存在我猜第3项是问题所在,如果是这样的话,在调用
File.Copy
之前,您需要确保目标目录存在。如果是这种情况,您将看到一个DirectoryNotFoundException
。您可以使用以确保目标目录存在。如果遇到问题,请尝试查看以下示例:
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = path + "temp";
try
{
// Create the file and clean up handles.
using (FileStream fs = File.Create(path)) {}
// Ensure that the target does not exist.
File.Delete(path2);
// Copy the file.
File.Copy(path, path2);
Console.WriteLine("{0} copied to {1}", path, path2);
// Try to copy the same file again, which should succeed.
File.Copy(path, path2, true);
Console.WriteLine("The second Copy operation succeeded, which was expected.");
}
catch
{
Console.WriteLine("Double copy is not allowed, which was not expected.");
}
}
}
摘自:反斜杠加倍是正确的, 我想你的问题是文件名。
尝试在不执行该操作的情况下读取文件,但在查看是否“隐藏已知类型的扩展名”之前,如果您这样做,则文件名应为test1.txt.txt:)要查看错误的具体原因,可以将代码包装在
try catch
块中:
try { // Code that can throw an exception }
catch (Exception ex)
{
// Show what went wrong
// Use Console.Write() if you are using a console
MessageBox.Show(ex.Message, "Error!");
}
最可能的问题是缺少源文件、目标文件夹不存在,或者您没有访问该位置的权限
在windows 7上,我注意到您需要管理员权限才能直接写入安装了操作系统的驱动器的根目录(通常是c:\
)。
尝试在该位置写入文件或创建目录可能会失败,因此我建议您使用另一个位置。异常消息和堆栈跟踪是什么?反斜杠加倍只是为了显示。您收到了什么错误消息。您可以发布异常文本吗?双反斜杠很好。它实际上不是一个双反斜杠-它只是在调试过程中出现的那样。我知道有点痛。你确定C驱动器的根目录上存在
test1.txt
?用于运行应用程序的帐户是否具有复制文件的权限?文件是否正在使用中(即在某个编辑器中打开)?4。C:\不存在/不可访问(我知道可能性很小)@SkonJeet第1项涵盖了这种可能性。您可以添加“隐藏已知文件扩展名”选项作为可能的故障模式。。。这就是问题所在(我的文件实际上是test.txt.txt)。谢谢大家!@事实上,列表中的第1项中包含了这一点。@David Heffernan-是的,但这取决于参考系统-脚本中不存在这一点,但我确实存在这一点。这肯定是一个注释,而不是答案!?它的建议与最高投票答案相同。。。并更详细地澄清了双反斜杠David Heffernan已经涵盖了问题的所有可能原因-您正要求OP提供更多信息以便这样做-但没有提到这可能是一个访问问题。OP评论中的其他人都在你的“答案”中涵盖了你的所有内容,但没有机会因为被标记为正确而获得代表。这似乎不公平。我试图用苏格拉底的方法来解释OP,答案不是必需的。没有一条注释完全解释了文本字符串和转义。就这样。如果你觉得这不公平,对不起,这不是我的意图。在这种情况下,苏格拉底提出的解决老年退休金问题的方法可以很容易地在评论中使用,而不是在回答中使用。道歉还是被接受了——编辑得很好。
try { // Code that can throw an exception }
catch (Exception ex)
{
// Show what went wrong
// Use Console.Write() if you are using a console
MessageBox.Show(ex.Message, "Error!");
}