2012-07-02 2 views
-5

C#을 사용하여 임시 파일을 생성하고 있습니다.FileIOException, file.length 예외가 throw되었습니다.

또한
public partial class frmResults : Form 
{ 
    public static string caseFile = ""; 

    private void frmResults_Load(object sender, EventArgs e) 
    { 
     caseFile = CreateTempFiles(); 
     FileInfo file = new FileInfo(caseFile + ".rpt"); 

     if (file.Exists) 
     { 
      try 
      { 
       if (fil.Length < 64000000) 
       { 
        richTabular.LoadFile(caseFile + ".rpt", RichTextBoxStreamType.PlainText); 
       } 
      } 
      catch (IOException io) 
      { 
       MessageBox.Show(io.GetType().Name); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 
     } 

     public static string CreateTempFiles() 
     { 
      string sPath; 
      caseFile = Path.GetTempFileName(); 

      sPath = Path.GetTempPath(); 
      string workDir = sPath + "\\work\\"; 
      // create work directory 
      if (!Directory.Exists(workDir)) 
      { 
       Directory.CreateDirectory(workDir); 
      } 
      // create temp file name 
      int i = 0; 
      string tmpfileprefix = workDir + "Rdp"; 
      string tmpfilename = ""; 
      do 
      { 
       i++; 
       tmpfilename = tmpfileprefix + i.ToString("D6"); 
      } while (File.Exists(tmpfilename + ".rpt")); 

      caseFile = tmpfilename; 
      return caseFile; 
     } 
    } 
} 

Error:fil.Length = 'fil.Length' threw an exception of type 'System.IO.FileNotFoundException' Could not find file 'C:\Users\sc\AppData\Local\Temp\\work\Rdp000001.rpt' .

, if (fil.Exists) false를 반환.

+2

무엇이 잘못되었는지를 이해하기 위해서는 오류 메시지가 너무 분명합니다. 해당 파일이 지정된 위치에 존재하는지 여부를 확인 했습니까? – Shyju

+0

그것은 나던 것을 확인했습니다. 하지만 확실하지 않습니다 그것을 만들지 않습니다 – user575219

+1

당신은 무엇을하려고합니까? 'CreateTempFiles()'는 항상 존재하지 않는 파일을 반환합니다. –

답변

2

오류 메시지가 더 명확 할 수 없습니다

Error:fil.Length = 'fil.Length' threw an exception of type 'System.IO.FileNotFoundException' Could not find file 'C:\Users\sc\AppData\Local\Temp\work\Rdp000001.rpt'.

그래서 ... 유효한 파일 객체 아니다, 그러나 당신이 그것을 할 것을 요구하는 방법 (길이) 때문에 예외가되었다 전화 던져. 파일이 존재하지 않습니다.

+0

y 임시 파일을 만들지 않습니다. – user575219

+0

@ user575219 : 두 개의 변수를 사용하고 있습니다. 'file' (존재하지 않음)과'fil'가 있습니다. 'fil'가 오류를 던지고 있습니다. 그게 어디서 오는거야? –

+0

괜찮아. 불공정 한 -3. 나는 그것을 올바르게 만들고 있지만 예외를 준 Streamwriter를 사용해서 쓰지는 않는다. – user575219

0

CreateTempFiles 메서드는 아직 작성되지 않은 임시 파일에만 파일 이름을 반환합니다. 이 메소드에서 파일을 만드는 위치는 없습니다. 또한이 메서드에서 기존 파일의 이름을 가져 오는 위치가 없으므로 항상 새 파일이됩니다. 그래도 안전을 원할 경우 if(File.Exists(caseFile + ".rpt")) 수표를 추가하여 길이를 확인할 수 있습니다.

관련 문제