コピー
.Net Framework1.1にはディレクトリをコピーする機能がない。
こんなんあるやろって探して見たがやはり見つからない。
単に探し方が悪いのかも...知っている方は教えてください。
ということで関数を作って見ました。
private void CopyDirectory(string nSourcePath, string nDestPath)
{
if( System.IO.Directory.Exists(nDestPath) ){
try{
// コピー先のディレクトリがなければ作成する
if (! System.IO.Directory.Exists(nDestPath)){
System.IO.Directory.CreateDirectory(nDestPath);
System.IO.File.SetAttributes(nDestPath,System.IO.File.GetAttributes(nSourcePath));
}
// コピー先の末尾に DirectorySeparatorChar がなければ加える
if (nDestPath[nDestPath.Length - 1] != System.IO.Path.DirectorySeparatorChar){
nDestPath += System.IO.Path.DirectorySeparatorChar;
}
// コピー元のディレクトリにあるすべてのファイルをコピーする
foreach (string nFilePath in System.IO.Directory.GetFiles(nSourcePath)){System.IO.File.Copy(nFilePath, nDestPath + System.IO.Path.GetFileName(nFilePath),true);
}
// コピー元のディレクトリをすべてコピーする (再帰)
foreach (string nDirPath in System.IO.Directory.GetDirectories(nSourcePath))
{
CopyDirectory(nDirPath, nDestPath + System.IO.Path.GetFileName(nDirPath));
}
}
}
catch( Exception e )
{
MessageBox.Show(e.Message);
}
}
こんなんあるやろって探して見たがやはり見つからない。
単に探し方が悪いのかも...知っている方は教えてください。
ということで関数を作って見ました。
private void CopyDirectory(string nSourcePath, string nDestPath)
{
if( System.IO.Directory.Exists(nDestPath) ){
try{
// コピー先のディレクトリがなければ作成する
if (! System.IO.Directory.Exists(nDestPath)){
System.IO.Directory.CreateDirectory(nDestPath);
System.IO.File.SetAttributes(nDestPath,System.IO.File.GetAttributes(nSourcePath));
}
// コピー先の末尾に DirectorySeparatorChar がなければ加える
if (nDestPath[nDestPath.Length - 1] != System.IO.Path.DirectorySeparatorChar){
nDestPath += System.IO.Path.DirectorySeparatorChar;
}
// コピー元のディレクトリにあるすべてのファイルをコピーする
foreach (string nFilePath in System.IO.Directory.GetFiles(nSourcePath)){System.IO.File.Copy(nFilePath, nDestPath + System.IO.Path.GetFileName(nFilePath),true);
}
// コピー元のディレクトリをすべてコピーする (再帰)
foreach (string nDirPath in System.IO.Directory.GetDirectories(nSourcePath))
{
CopyDirectory(nDirPath, nDestPath + System.IO.Path.GetFileName(nDirPath));
}
}
}
catch( Exception e )
{
MessageBox.Show(e.Message);
}
}
