2016-05-25 25 views
1

ファイルをアップロードしてその有効性をチェックする次のコードがあります。 まず問題:ファイルパスの文字制限C#

if (RadUpload1.UploadedFiles.Count == 0) 
{ 
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
     ErrorsList.ERR_P_DATE_FILE_VALID); 
} 
else 
{ 
    if (RadUpload1.UploadedFiles.Count > 0) 
    { 
     foreach (UploadedFile validFile in RadUpload1.UploadedFiles) 
     { 
      FileInfo fi = new FileInfo(validFile.FileName); 
      Stream fs = validFile.InputStream; 

      IDbContextualRecord pFile = statusContext.CreateAndAddRecordForInsert(PartStoredFile.t_name); 
      pFile[PartStoredFile.c_partId] = _part[Part.c_id]; 
      string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] + 
            "\\partRec\\" + _part[Part.c_id] + "\\" + pFile[PartStoredFile.c_id]; 

      long bytesOnTheStream = 0L; 
      try 
      { 
       DirectoryInfo dir = new DirectoryInfo(targetFolder); 
       if (dir.Exists == false) 
        dir.Create(); 

       string fullFileName = Path.Combine(targetFolder, fi.Name); 

       Stream targetStream = File.OpenWrite(fullFileName); 
       byte[] buffer = new Byte[AppConstants.BUFF_SIZE]; 
       int bytesRead; 

       // while the read method returns bytes 
       // keep writing them to the output stream 
       while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0) 
       { 
        targetStream.Write(buffer, 0, bytesRead); 
        bytesOnTheStream += bytesRead; 
       } 

       fs.Close(); 
       targetStream.Close(); 
      } 
      catch (Exception ex) 
      { 
       throw ex; 
      } 

私がやりたいことは、ファイルパス名の文字数は私にエラーのメッセージを表示するには、260を超えるかどうかを確認することです。

これは、変更が行われた後の第2の問題である:

if (RadUpload1.UploadedFiles.Count <= 0) 
{ 
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
            ErrorsList.ERR_P_DATE_FILE_VALID); 
} 
else 
{ 
    if (RadUpload1.UploadedFiles.Count > 0) 
    { 
     foreach (UploadedFile validFile in RadUpload1.UploadedFiles) 
     { 
      pomDoc = (IDbContextualRecord)Session[AppConstants.POM_DOCUMENT_NEW]; 

      FileInfo fi = new FileInfo(validFile.FileName); 
      Stream fs = validFile.InputStream; 

      IDbContextualRecord pomFile = pomContext.CreateAndAddRecordForInsert(PomFile.t_name); 
      pomFile[PomFile.c_pomDocumentId] = pomDoc[PomDocument.c_id]; 
      string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] + "\\POM\\" + pomDoc[PomDocument.c_id] + "\\" + pomFile[PomFile.c_id]; 

      long bytesOnTheStream = 0L; 
      try 
      { 
       DirectoryInfo dir = new DirectoryInfo(targetFolder); 
       if (dir.Exists == false) 
        dir.Create(); 

       string fullFileName = Path.Combine(targetFolder, fi.Name); 

       if (fullFileName.Length > 260) 
       { 
        throw new Exception(string.Format("The filename is too long!",fullFileName)); 
       } 
       Stream targetStream = File.OpenWrite(fullFileName); 
       byte[] buffer = new Byte[AppConstants.BUFF_SIZE]; 
       int bytesRead; 

       // while the read method returns bytes 
       // keep writing them to the output stream 
       while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0) 
       { 
        targetStream.Write(buffer, 0, bytesRead); 
        bytesOnTheStream += bytesRead; 
       } 

       fs.Close(); 
       targetStream.Close(); 
      } 
      catch (Exception ex) 
      { 
       throw ; 
      } 
+2

問題は何ですか? fullFileName.Lenthは必要なものではありませんか? – 3615

+0

が正しい。単にstring.length関数を使用します。 – DDave

+0

tryブロックの後に書きますか?何もしないから。 – Nomonom

答えて

1

は、あなただけの260にfullFileName.Lenghtを比較し、必要に応じて例外を発生する必要があります。

また
if (RadUpload1.UploadedFiles.Count <= 0) // Changed the condition to remove the check within the else block 
{ 
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
     ErrorsList.ERR_P_DATE_FILE_VALID); 
} 
else 
{ 
    foreach (UploadedFile validFile in RadUpload1.UploadedFiles) 
    { 
     FileInfo fi = new FileInfo(validFile.FileName); 
     Stream fs = validFile.InputStream; 

     IDbContextualRecord pFile = statusContext.CreateAndAddRecordForInsert(PartStoredFile.t_name); 
     pFile[PartStoredFile.c_partId] = _part[Part.c_id]; 
     string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] + 
           "\\partRec\\" + _part[Part.c_id] + "\\" + pFile[PartStoredFile.c_id]; 

     long bytesOnTheStream = 0L; 
     try 
     { 
      DirectoryInfo dir = new DirectoryInfo(targetFolder); 
      if (dir.Exists == false) 
       dir.Create(); 

      string fullFileName = Path.Combine(targetFolder, fi.Name); 

      if(fullFileName.Length > 260) 
      { 
       throw new Exception(string.Format("The filename {0} is too long.", fullFileName)); 
       // Or do whatever you want 
      } 

      Stream targetStream = File.OpenWrite(fullFileName); 
      byte[] buffer = new Byte[AppConstants.BUFF_SIZE]; 
      int bytesRead; 

      // while the read method returns bytes 
      // keep writing them to the output stream 
      while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0) 
      { 
       targetStream.Write(buffer, 0, bytesRead); 
       bytesOnTheStream += bytesRead; 
      } 

      fs.Close(); 
      targetStream.Close(); 
     } 
     catch (Exception ex) 
     { 
      throw; 
     } 

、そうでありませんthrow ex;にする必要がありますが、throw;にするか、スタックトレースをリセットします。Is there a difference between "throw" and "throw ex"?

+0

例外をスローする必要があります:新しい例外をスローする(string.Format( "ファイル名{0}が長すぎます。"、fullFileName));それともメッセージだけを表示できますか? – Nomonom

+0

まあ、私は書いた、ちょうど例外の下で* '/ /あなたがしたいものは何でも' * ... @Nomonom –

+0

私は同じ問題でファイルする必要があるので尋ねます。 1つはメッセージとして例外が表示され、もう1つはコードで例外が表示されます。 – Nomonom