■ USB 케이블 연결 카메라에서 사진 파일을 복사하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
using System; using System.Collections.Generic; using System.IO; using MediaDevices; namespace TestProject { /// <summary> /// 프로그램 /// </summary> class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> private static void Main() { string cameraName = "Canon PowerShot SX230 HS"; string sourceRootDirectoryPath = @"\\이동식 저장소\DCIM"; string targetRootDirectoryPath = @"D:\Camera"; IEnumerable<MediaDevice> mediaDeviceEnumerable = MediaDevice.GetDevices(); foreach(MediaDevice mediaDevice in mediaDeviceEnumerable) { Console.WriteLine($"CAMERA NAME : {mediaDevice.FriendlyName}"); if(mediaDevice.FriendlyName.Contains(cameraName)) { mediaDevice.Connect(); string[] sourceDirectoryPathArray = mediaDevice.GetDirectories(sourceRootDirectoryPath); foreach(string sourceDirectoryPath in sourceDirectoryPathArray) { Console.WriteLine($" SOURCE DIRECTORY PATH : {sourceDirectoryPath}"); Console.WriteLine(); string sourceDirectoryName = sourceDirectoryPath.TrimEnd('\\'); sourceDirectoryName = sourceDirectoryName.Substring(sourceDirectoryName.LastIndexOf('\\') + 1); string[] sourceFilePathArray = mediaDevice.GetFiles(sourceDirectoryPath); foreach(string sourceFilePath in sourceFilePathArray) { Console.WriteLine($" SOURCE FILE PATH : {sourceFilePath}"); MemoryStream memoryStream = new MemoryStream(); mediaDevice.DownloadFile(sourceFilePath, memoryStream); memoryStream.Position = 0; string sourceFileName = sourceFilePath.TrimEnd('\\'); sourceFileName = sourceFileName.Substring(sourceFileName.LastIndexOf('\\') + 1); string targetDirectoryPath = Path.Combine(targetRootDirectoryPath, sourceDirectoryName); if(!Directory.Exists(targetDirectoryPath)) { Directory.CreateDirectory(targetDirectoryPath); } string targetFilePath = Path.Combine(targetRootDirectoryPath, sourceDirectoryName, sourceFileName); Console.WriteLine($" TARGET FILE PATH : {targetFilePath}"); Console.WriteLine(); CopyFile(memoryStream, targetFilePath); } } } } } #endregion #region 파일 복사하기 - CopyFile(sourceStream, targetFilePath) /// <summary> /// 파일 복사하기 /// </summary> /// <param name="sourceStream">소스 스트림</param> /// <param name="targetFilePath">타겟 파일 경로</param> private static void CopyFile(Stream sourceStream, string targetFilePath) { using(FileStream targetStream = new FileStream(targetFilePath, FileMode.Create, FileAccess.Write)) { byte[] targetByteArray = new byte[sourceStream.Length]; sourceStream.Read(targetByteArray, 0, (int)sourceStream.Length); targetStream.Write(targetByteArray, 0, targetByteArray.Length); sourceStream.Close(); } } #endregion } } |
※ 패키지 설치 : MediaDevices
1. 비주얼 스튜디오를 실행한다.
2. 비주얼 스튜디오에서 [도구] / [NuGet 패키지 관리자] / [패키지 관리자 콘솔] 메뉴를 클릭한다.
3. [패키지 관리자 콘솔]에서 아래 명령을 실행한다.
▶ 실행 명령
1 2 3 |
Install-Package MediaDevices |