728x90

 public string File_SAVE(string fileName)
        {

            //경로에 파일이 있는지 여부 체크
            if (string.IsNullOrWhiteSpace(fileName)) { return null; }

 

            //저장경로 설정
            string dirPath = AppDomain.CurrentDomain.BaseDirectory;
            string savePath = Path.Combine(dirPath, fileName);

 

            //저장경로에 디렉토리 없으면 만들어줌
            if (!Directory.Exists(dirPath))
                Directory.CreateDirectory(dirPath);
          
            try
            {
                byte[] file;

                using (var stream = new FileStream(dFileName, FileMode.Open, FileAccess.Read))
                {
                    using (var reader = new BinaryReader(stream))
                    {
                        file = reader.ReadBytes((int)stream.Length);
                    }
                }
                excute.GetDS("UPDATE PLM_DOCUMENT SET PDT_FILEE = CONVERT(varbinary,'" + file + "') WHERE PDT_ID = 3");


                //파일스트림으로 읽고
                using (FileStream fsSource = new FileStream(dFileName, FileMode.Open, FileAccess.Read))
                {
                    byte[] bytes = new byte[fsSource.Length];
                    int numBytesToRead = (int)fsSource.Length;
                    int numBytesRead = 0;

                    while (numBytesToRead > 0)
                    {
                        int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);

                        if (n == 0)
                            break;

                        numBytesRead += n;
                        numBytesToRead -= n;
                    }
                    numBytesToRead = bytes.Length;
   

                     //파일스트림으로 저장
                    using (FileStream fsNew = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                    {
                        fsNew.Write(bytes, 0, numBytesToRead);
                        fsNew.Close();
                        fsNew.Dispose();
                    }
                    fsSource.Close();
                    fsSource.Dispose();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return null;
            }

            return savePath;
        }

728x90

'C#' 카테고리의 다른 글

C# .NET 사용자 정의 컨트롤 만들기  (1) 2024.01.25
C# 파일 열기  (0) 2021.06.10
C# MdiChildren 폼 호출시 화면 깜빡이는현상  (0) 2021.03.12
C# MdiChildren 폼 호출  (0) 2021.03.12
C# ini 파일 읽고 쓰기  (0) 2020.12.15

+ Recent posts