예제 #1
0
        public static FileInfo MoveTo(this FileInfo obj,
                                      FileInfo destination,
                                      bool overwrite)
#endif
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }

            if (null == destination)
            {
                throw new ArgumentNullException("destination");
            }

            obj.Refresh();
#if NET20
            if (FileSystemInfoExtensionMethods.NotFound(obj))
#else
            if (obj.NotFound())
#endif
            {
                throw new FileNotFoundException(obj.FullName);
            }

            if (!overwrite)
            {
                destination.Refresh();
                if (destination.Exists)
                {
#if NET20
                    throw new IOException(StringExtensionMethods.FormatWith("{0} already exists.", destination.FullName));
#else
                    throw new IOException("{0} already exists.".FormatWith(destination.FullName));
#endif
                }
            }

            var modified = obj.LastWriteTimeUtc;
            obj.MoveTo(destination.FullName);

            destination.Refresh();
            destination.LastWriteTimeUtc = modified;

            return(destination);
        }
예제 #2
0
        public static bool FixNewLine(this FileInfo file)
#endif
        {
            if (null == file)
            {
                throw new ArgumentNullException("file");
            }

#if NET20
            if (FileSystemInfoExtensionMethods.NotFound(file))
#else
            if (file.NotFound())
#endif
            {
                throw new FileNotFoundException(file.FullName);
            }

            var changed = false;
            using (var temp = new TempFile())
            {
                using (var tempStream = File.Open(temp.Info.FullName, FileMode.Open, FileAccess.Write, FileShare.Read))
                {
                    using (var tempWriter = new StreamWriter(tempStream))
                    {
                        using (var stream = File.Open(file.FullName, FileMode.Open, FileAccess.Read, FileShare.Read))
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                var cr = false;
                                while (true)
                                {
                                    var i = reader.Read();
                                    if (-1 == i)
                                    {
                                        break;
                                    }

                                    switch (i)
                                    {
                                    case '\r':
                                        cr = true;
                                        break;

                                    case '\n':
                                        tempWriter.Write("\r\n");
                                        if (cr)
                                        {
                                            cr = false;
                                        }
                                        else
                                        {
                                            changed = true;
                                        }

                                        break;

                                    default:
                                        if (cr)
                                        {
                                            tempWriter.Write("\r");
                                            cr = false;
                                        }

                                        tempWriter.Write((char)i);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                if (changed)
                {
                    temp.Info.CopyTo(file.FullName, true);
                }
            }

            return(changed);
        }