我想做一些简单的事情。获取文件夹A的内容并将文件和文件夹移动到文件夹A中的文件夹中。然后隐藏文件夹A.
下面的代码是找不到hiddenTarget的例外。
Directory.Create(hiddenTarget) is not helping
必须有一种简单的方法来做到这一点。目前我正在尝试创建一个临时目录。将当前目录中的所有文件放入其中。然后将临时目录移动到当前目录。然后隐藏当前目录。
以下是有问题的代码..
string tempFolder = Path.Combine(Environment.ExpandEnvironmentVariables("%TEMP%"), "tempTarget");
//Directory.CreateDirectory(tempFolder);
Directory.Move(currentTarget, tempFolder);
string hiddenTarget = Path.Combine(currentTarget, @".bak");
//Directory.CreateDirectory(hiddenTarget);
Directory.Move(tempFolder, hiddenTarget);
DirectoryInfo di = new DirectoryInfo(currentTarget);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
答案 0 :(得分:2)
首先,你有两个问题是你的隐藏目标不能以'。'开头。因为正如评论中指出的那样在NTFS中是非法的。
正如您所见,文件资源管理器不喜欢这种语法。
正如@Dour High Arch所指出的,这里是相关信息的链接:Naming Files, Paths, and Namespaces
您的下一个问题是该移动正在破坏原始目录结构。因此,您的步骤需要如下:
1)移动到临时目录,以避免两个进程(文件系统和您的进程)争用访问的任何问题。
2)由于第1步中的Directory.Move破坏了原始源目录。重新创建已销毁的源文件夹。
3)然后进入所需的嵌套文件夹。此移动操作将自动创建所需的子目录。步骤#2是必需的,因为无论出于何种原因我还在查看Directory.Move无法在没有源目录的情况下自动创建结构。
string currentTarget = @"C:\A";
string hiddenTarget = @"C:\A\Subfolder";
string tempTarget = @"C:\Temp";
Directory.Move(currentTarget, tempTarget);
Directory.CreateDirectory(currentTarget);
Directory.Move(tempTarget, hiddenTarget);
DirectoryInfo di = new DirectoryInfo(currentTarget);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
<强>更新强>
从工程角度来看,如果您真的关心要移动的数据,那么您真的应该执行复制。它可能会更慢,但有助于防止任何可怕的事情发生在数据上。在创建这些目录之前,您应该先检查这些目录是否存在。此示例中的异常处理也至少也是如此。我在这里要强调的是处理正在小心移动的数据!
static void Main(string[] args)
{
string sourceDir = @"C:\Src";
string tempDir = @"C:\Temp";
string destDir = Path.Combine(sourceDir, "Dest");
// Could optionally check to verify that the temp directory already exists here and destroy it if it does.
// Alternatively, pick a really unique name for the temp directory by using a GUID, Thread Id or something of that nature.
// That way you can be sure it does not already exist.
// Copy to temp, then destroy source files.
CopyDirectory(sourceDir, tempDir);
Directory.Delete(sourceDir, true);
// Copy to dest
CopyDirectory(tempDir, destDir);
// Hide the source directory.
DirectoryInfo di = new DirectoryInfo(sourceDir);
di.Attributes = FileAttributes.Directory | FileAttributes.Hidden;
// Clean up the temp directory that way copies of the files aren't sitting around.
// NOTE: Be sure to do this last as if something goes wrong with the move the temp directory will still exist.
Directory.Delete(tempDir, true);
}
/// <summary>
/// Recursively copies all subdirectories.
/// </summary>
/// <param name="sourceDir">The source directory from which to copy.</param>
/// <param name="destDir">The destination directory to copy content to.</param>
static void CopyDirectory(string sourceDir, string destDir)
{
var sourceDirInfo = new DirectoryInfo(sourceDir);
if (!sourceDirInfo.Exists)
{
throw new DirectoryNotFoundException($"Source directory does not exist or could not be found: '{sourceDir}'");
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDir))
{
Directory.CreateDirectory(destDir);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = sourceDirInfo.GetFiles();
foreach (FileInfo file in files)
{
string tempPath = Path.Combine(destDir, file.Name);
file.CopyTo(tempPath, false);
}
// Copy subdirectories
DirectoryInfo[] subDirs = sourceDirInfo.GetDirectories();
foreach (DirectoryInfo subdir in subDirs)
{
string tempPath = Path.Combine(destDir, subdir.Name);
CopyDirectory(subdir.FullName, tempPath);
}
}
以下是有关上述内容的更多链接。
MSDN - How to: Copy Directories
Stack Overflow - How to copy the entire contents of directory in C#?