Nomonom
Nomonom

Reputation: 145

Character Limit in filepath C#

I have the following piece of code which uploads a file and checks its validity. First issue:

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;
            }

What I want to do is to check if the number of characters in the filepath name exceeds 260 to display me a message of error.

This is the second issue after the modification was made:

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 ;
            }

Upvotes: 1

Views: 103

Answers (1)

Thomas Ayoub
Thomas Ayoub

Reputation: 29431

You just have to compare fullFileName.Lenght to 260 and raise an exception if needed:

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;
        }

Also, you don't want to throw ex; but rather throw; or this will reset the stacktrace, see Is there a difference between "throw" and "throw ex"?

Upvotes: 1

Related Questions