Program/Unity

유니티(Unity) USB 드라이브 텍스트 읽기

soccerda 2024. 8. 13. 08:11
반응형
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System.Text.RegularExpressions;

public class ReadTextFileFromUSB : MonoBehaviour
{
    void Start()
    {
        // 모든 드라이브 문자 가져오기
        DriveInfo[] allDrives = DriveInfo.GetDrives();

        foreach (DriveInfo drive in allDrives)
        {
            // USB 드라이브인지 확인
            if (drive.DriveType == DriveType.Removable)
            {
                string path = drive.Name + "a.txt";

                // 파일이 존재하는지 확인
                if (File.Exists(path))
                {
                    // 파일의 모든 텍스트를 읽어옴
                    string fileContents = File.ReadAllText(path);

                    // KEY 값 추출
                    string key = ExtractKey(fileContents);

                    if (!string.IsNullOrEmpty(key))
                    {
                        // 추출한 KEY 값을 콘솔에 출력
                        Debug.Log("Extracted KEY: " + key);
                    }
                    else
                    {
                        Debug.LogError("KEY 값을 찾을 수 없습니다.");
                    }
                    return; // 파일을 찾았으므로 더 이상 검사하지 않음
                }
            }
        }

        Debug.LogError("USB 드라이브에서 파일을 찾을 수 없습니다.");
    }

    string ExtractKey(string text)
    {
        // 정규식을 사용하여 KEY 값 추출
        Match match = Regex.Match(text, @"KEY=""([^""]+)""");
        if (match.Success)
        {
            return match.Groups[1].Value;
        }
        return null;
    }
}

 

반응형