Esempio n. 1
0
        /// <summary>
        /// Creates a path that will no overwrite any files that already exist at
        /// that location.
        ///
        /// To ensure this, a zero-length file is created and its full path is returned.
        /// </summary>
        /// <param name="path">The path requested</param>
        /// <returns>The path that contains the new zero-length file</returns>
        public static string GetNonConflictingPath(string path)
        {
            string directory = Path.GetDirectoryName(path);
            string fileName  = Path.GetFileName(path);

            return(TempFileManager.CreateNewFile(directory, fileName, false));
        }
        /*
         * public int MaxChildPath(StringBuilder buffer)
         * {
         *      buffer.Append(Path.DirectorySeparatorChar);
         *      buffer.Append(name);
         *
         *      int startPos = buffer.Length;
         *      int currLen = 0;
         *      int depth = 0;
         *
         *      StringBuilder maxChildPath = new StringBuilder();
         *      StringBuilder currChildPath = new StringBuilder();
         *      foreach (TransientFileSystemItem child in children)
         *      {
         *              int tmpDepth = child.MaxChildPath(currChildPath);
         *              if (currChildPath.Length > currLen && tmpDepth > depth)
         *              {
         *                      if (currLen != 0)
         *                      {
         *                              buffer.Remove(startPos, currLen);
         *                      }
         *                      buffer.Append(currChildPath.ToString());
         *                      currLen = currChildPath.Length;
         *                      depth = tmpDepth;
         *              }
         *              else
         *              {
         *                      currChildPath.Remove(0, currChildPath.Length);
         *              }
         *              Debug.Assert(currChildPath.Length == 0, "Programming error: currChildPath != 0");
         *      }
         *      if (maxChildPath.Length > 0)
         *      {
         *              buffer.Append(maxChildPath.ToString());
         *      }
         *
         *      return ++depth;
         * }
         */

        public FileSystemInfo Create(DirectoryInfo destination)
        {
            // TODO use constant instead of literal 260
            int charsLeft   = 247 - destination.FullName.Length;
            int maxElements = MaxElementsOfPathLongerThan(charsLeft);

            if (charsLeft <= 0)
            {
                throw new PathTooLongException();
            }

            string truncatedName;

            if (maxElements > 0)
            {
                int charsForMe;
                if (maxElements > 1)
                {
                    charsForMe = Math.Max(1, (int)Math.Floor(((double)(charsLeft - 6) / (maxElements - 1))));
                }
                else
                {
                    charsForMe = Math.Max(1, (int)Math.Floor(((double)(charsLeft - 1) / maxElements)));
                }

                if (charsForMe >= name.Length)
                {
                    truncatedName = name;
                }
                else
                {
                    truncatedName = name.Substring(0, charsForMe);
                }
            }
            else
            {
                truncatedName = name;
            }

            string fullPath = Path.Combine(destination.FullName, truncatedName);

            Debug.WriteLine("TransientDirectory: Want to create " + fullPath);
            string createdPath = TempFileManager.CreateNewFile(destination.FullName, truncatedName, true);

            DirectoryInfo dirInfo = new DirectoryInfo(createdPath);

            if (children != null)
            {
                foreach (TransientFileSystemItem item in children)
                {
                    if (item != null)
                    {
                        item.Create(dirInfo);
                    }
                }
            }
            return(dirInfo);
        }
Esempio n. 3
0
        public FileSystemInfo Create(DirectoryInfo destination)
        {
            string truncatedName;

            // TODO: use constant instead of literal 260
            int charsLeft = 247 - destination.FullName.Length;

            if (charsLeft < name.Length + 1)
            {
                truncatedName = Path.GetExtension(name);

                if (truncatedName == null)
                {
                    truncatedName = string.Empty;
                }

                int charsForName = charsLeft - truncatedName.Length /*extension*/ - 1 /*path seperator*/;
                if (charsForName < 1)
                {
                    throw new PathTooLongException();
                }
                truncatedName = Path.GetFileNameWithoutExtension(name).Substring(0, charsForName) + truncatedName;
            }
            else
            {
                truncatedName = name;
            }

            // create the file
            Debug.WriteLine("TransientFile: Want to create " + Path.Combine(destination.FullName, truncatedName));
            string newFile = TempFileManager.CreateNewFile(destination.FullName, truncatedName, false);

            lock (this)
            {
                using (data)
                {
                    using (FileStream fs = File.Create(newFile))
                    {
                        int b;
                        while ((b = data.ReadByte()) != -1)
                        {
                            fs.WriteByte((byte)b);
                        }
                    }
                }
            }

            return(new FileInfo(newFile));
        }
        static TempFileManager()
        {
            string prefix = ProcessHelper.GetCurrentProcessName();
            int lastDot = prefix.LastIndexOf('.');
            if (lastDot > -1)
            {
                prefix = prefix.Substring(0, lastDot);
            }
            string currentDir = Environment.CurrentDirectory;
            string suffix;
            if (currentDir == null)
                suffix = "";
            else
                suffix = currentDir.GetHashCode().ToString(CultureInfo.InvariantCulture);

            singleton = new TempFileManager(Path.Combine(Path.GetTempPath(), prefix + suffix), true);
        }
        static TempFileManager()
        {
            string prefix  = ProcessHelper.GetCurrentProcessName();
            int    lastDot = prefix.LastIndexOf('.');

            if (lastDot > -1)
            {
                prefix = prefix.Substring(0, lastDot);
            }
            string currentDir = Environment.CurrentDirectory;
            string suffix;

            if (currentDir == null)
            {
                suffix = "";
            }
            else
            {
                suffix = currentDir.GetHashCode().ToString(CultureInfo.InvariantCulture);
            }

            singleton = new TempFileManager(Path.Combine(Path.GetTempPath(), prefix + suffix), true);
        }