filestream.How를 사용하여 한 폴더에서 다른 폴더로 파일을 복사하고 싶습니다.이 파일은 achived.when 일 때 file.copy를 사용하려고하면이 파일을 다른 사람이 사용하고 있습니다. 프로세스,이 피하기 위해 나는 C#을 사용하여 파일 스트림을 사용하고 싶다. 어떤 폴더에서 다른 폴더로 파일을 복사 할 수있는 샘플을 제공 할 수 있습니까?C에서 파일 복사에 filestream을 사용하는 방법 #
1
A
답변
1
가 복사에 내가 코드를 아래에 사용 : -
public static void Copy(string inputFilePath, string outputFilePath)
{
int bufferSize = 1024 * 1024;
using (FileStream fileStream = new FileStream(outputFilePath, FileMode.OpenOrCreate, FileAccess.Write,FileShare.ReadWrite))
//using (FileStream fs = File.Open(<file-path>, FileMode.Open, FileAccess.Read, FileShare.Read))
{
FileStream fs = new FileStream(inputFilePath, FileMode.Open, FileAccess.ReadWrite);
fileStream.SetLength(fs.Length);
int bytesRead = -1;
byte[] bytes = new byte[bufferSize];
while ((bytesRead = fs.Read(bytes, 0, bufferSize)) > 0)
{
fileStream.Write(bytes, 0, bytesRead);
}
}
}
1
string fileName = "Mytest.txt";
string sourcePath = @"C:\MyTestPath";
string targetPath = @"C:\MyTestTarget";
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
{
System.IO.Directory.CreateDirectory(targetPath);
}
// To copy a file to another location and
// overwrite the destination file if it already exists.
System.IO.File.Copy(sourceFile, destFile, true);
+3
스트리밍을 사용하지 않으므로 필요합니다. –
[? File.Copy 대 파일을 복사하는 가장 안전한 방법하여 FileStream인가]의 사용 가능한 복제 (http://stackoverflow.com/ 질문/4959349/is-filestream-the-safe-way-to-copy-a-file-vs-file-copy) – rbr94
[FileStream 클래스를 사용하여 파일을 복사 할 때 출력이 입력과 일치하지 않는 이유는 무엇입니까?] : //stackoverflow.com/questions/3427185/using-filestream-classes-to-copy-files-why-does-output-not-match-input) – JeetDaloneboy
이 링크를 보시길 바랍니다. http://stackoverflow.com/questions/1246899/file-copy-vs-manual-filestr eam-write-for-copying-file – vijesh