728x90
728x170
▶ Program.cs
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. [패키지 관리자 콘솔]에서 아래 명령을 실행한다.
Install-Package MediaDevices
728x90
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON] Process 클래스 : Start 정적 메소드를 사용해 장치 및 프린터 대화 상자 표시하기 (0) | 2021.02.23 |
---|---|
[C#/COMMON] Process 클래스 : Start 정적 메소드를 사용해 장치 및 프린터 대화 상자 표시하기 (0) | 2021.02.23 |
[C#/COMMON] Process 클래스 : Start 정적 메소드를 사용해 장치 및 프린터 대화 상자 표시하기 (0) | 2021.02.23 |
[C#/COMMON] 카메라 정보 조회하기 (0) | 2021.02.23 |
[C#/COMMON] Process 클래스 : 프로세스 파일 경로 구하기 (0) | 2021.02.23 |
[C#/COMMON] 누겟 설치 : MediaDevices (0) | 2021.02.22 |
[C#/COMMON] Process 클래스 : PowerShell 스크립트 파일 실행하기 (0) | 2021.02.22 |
[C#/COMMON] Process 클래스 : PowerShell 스크립트 실행하기 (0) | 2021.02.22 |
[C#/COMMON] Runspace 클래스 : PowerShell 스크립트 실행하기 (0) | 2021.02.22 |
[C#/COMMON] 극 좌표(Polar Coordinates) 사용하기 (0) | 2021.02.14 |