I try to write a console app C# to move my etxt files to another folder. The functions just copies certain .txt files from folder A to folder AA
string source = "C:\\A\\ResultClassA.txt";
File.Move(Source, "C:\\AA");
But its always giving this error message:
Access to the path is denied.
Troubleshooting tips: Make sure you have sufficient privileges to access this resource. If you are attempting to access a file, make sure it is not ReadOnly. Get general help for this exception.
Do i really need to set my folder开发者_如何学编程 A and folder B to "NOT ReadOnly" attribute before "File.move" code are execuse? and set to read only back after success moved?
Thanks. By Hero .
You need to specify the full path and make sure the path C:\AA
exists
string source = "C:\\A\\ResultClassA.txt";
File.Move(Source, "C:\\AA\\ResultClassA.txt");
See here for good sample
using System;
using System.IO;
class Test
{
public static void Main()
{
string path = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp2\MyTest.txt";
try
{
if (!File.Exists(path))
{
// This statement ensures that the file is created,
// but the handle is not kept.
using (FileStream fs = File.Create(path)) {}
}
// Ensure that the target does not exist.
if (File.Exists(path2))
File.Delete(path2);
// Move the file.
File.Move(path, path2);
Console.WriteLine("{0} was moved to {1}.", path, path2);
// See if the original exists now.
if (File.Exists(path))
{
Console.WriteLine("The original file still exists, which is unexpected.");
}
else
{
Console.WriteLine("The original file no longer exists, which is expected.");
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}
Hero, you are moving from a file name to a folder name, try to specify a file name with extension inside the C:\AA
folder.
does AA exist already on C ?
精彩评论