다음 코드에서는 Excel 파일이 열려 있는지 확인하려고합니다. 파일을 닫으면 파일을 닫을 때 닫히지 마, 도와 줄 수있어?이미 열려있는 Excel 파일을 닫으면
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Runtime.InteropServices; using System.Windows.Forms; using Microsoft.Office.Interop.Excel; using Excel = Microsoft.Office.Interop.Excel; namespace CloseIfFileOpen { class Program { public static void Main() { Excel.Application oApp; Excel.Workbook oBook; oApp = new Excel.Application(); oBook =oApp.Workbooks.Add(@"C:\Users\user\Documents\WEF\Excel\Example.xlsx"); string filePath; filePath = @"C:\Users\user\Documents\WEF\Excel\Example.xlsx"; try { using (File.Open(filePath, FileMode.Open)) { } } catch (IOException e) { var errorCode = Marshal.GetHRForException(e) & ((1 << 16) - 1); //return errorCode == 32 || errorCode == 33; MessageBox.Show("the file is unavailable now"); oBook.Save(); oBook.Close(); oApp.Quit(); } } } }
도움을 드리고 싶지만 코드를 읽을 수 없습니다. 이름 지정은 끔찍하고 의견도없고 지저분합니다. – Noctis