Changeset 1860


Ignore:
Timestamp:
03/01/10 05:46:03 (5 years ago)
Author:
lowjoel
Message:

Generate names which are valid, not any of those reserved file names (e.g. con, prn, nul)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/6.0/Eraser.Manager/FileSystem.cs

    r1742 r1860  
    2424using System.Collections.Generic; 
    2525using System.Text; 
     26using System.IO; 
    2627using Eraser.Util; 
    27 using System.IO; 
    2828 
    2929namespace Eraser.Manager 
     
    4444        public static string GenerateRandomFileName(DirectoryInfo info, int length) 
    4545        { 
    46             //Get a random file name 
    47             Prng prng = PrngManager.GetInstance(ManagerLibrary.Settings.ActivePrng); 
     46            //Initialise the base name, if any. 
    4847            string resultPrefix = info == null ? string.Empty : info.FullName + 
    4948                Path.DirectorySeparatorChar; 
     49 
     50            //Variables to store the intermediates. 
    5051            byte[] resultAry = new byte[length]; 
    5152            string result = string.Empty; 
     53            List<string> prohibitedFileNames = new List<string>(new string[] { 
     54                "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4", 
     55                "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3", 
     56                "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9" 
     57            }); 
    5258 
    5359            do 
     
    6470                result = Encoding.UTF8.GetString(resultAry); 
    6571            } 
    66             while (info != null && (Directory.Exists(resultPrefix + result) || 
    67                 System.IO.File.Exists(resultPrefix + result))); 
     72            while (info != null && 
     73                prohibitedFileNames.IndexOf(Path.GetFileNameWithoutExtension(result)) != -1 || 
     74                (Directory.Exists(resultPrefix + result) || File.Exists(resultPrefix + result))); 
    6875            return resultPrefix + result; 
    6976        } 
Note: See TracChangeset for help on using the changeset viewer.