示例#1
0
        public static FileInfo Truncate(this FileInfo obj,
                                        string value)
#endif
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }

            using (var stream = obj.Open(FileMode.Truncate, FileAccess.Write, FileShare.Read))
            {
                using (var writer = new StreamWriter(stream))
                {
#if NET20
                    if (ObjectExtensionMethods.IsNotNull(value))
#else
                    if (value.IsNotNull())
#endif
                    {
                        writer.Write(value);
                    }
                }
            }

            return(obj);
        }
        private static string[] ToStringArray(FileSystemInfo obj,
                                              IEnumerable <object> paths)
        {
            if (null == obj)
            {
                throw new ArgumentNullException("obj");
            }

            var args = new List <string>
            {
                obj.FullName
            };

#if NET20
            if (ObjectExtensionMethods.IsNotNull(paths))
#else
            if (paths.IsNotNull())
#endif
            {
                var range = paths
#if NET20
                            .Where(x => ObjectExtensionMethods.IsNotNull(x))
#else
                            .Where(x => x.IsNotNull())
#endif
                            .Select(path => path.ToString().RemoveIllegalFileCharacters());
                args.AddRange(range);
            }

            return(args.ToArray());
        }
示例#3
0
        private StreamWriter Item(FileInfo file,
                                  string firstLine,
                                  FileMode mode,
                                  FileAccess access,
                                  FileShare share)
        {
            if (Disposed)
            {
                throw new InvalidOperationException("This object has been disposed.");
            }

            if (!ContainsKey(file.FullName))
            {
                var exists = file.Exists;
                var writer = new StreamWriter(file.Open(mode, access, share), Encoding.UTF8);
                Add(file.FullName, writer);
#if NET20
                if (ObjectExtensionMethods.IsNotNull(firstLine))
#else
                if (firstLine.IsNotNull())
#endif
                {
                    switch (mode)
                    {
                    case FileMode.Append:
                        if (!exists)
                        {
                            writer.WriteLine(firstLine);
                        }

                        break;

                    default:
                        writer.WriteLine(firstLine);
                        break;
                    }
                }
            }

            return(this[file.FullName]);
        }
示例#4
0
        protected virtual void Dispose(bool disposing)
        {
            if (!Disposed && disposing)
            {
#if NET20
                if (ObjectExtensionMethods.IsNotNull(Info))
#else
                if (Info.IsNotNull())
#endif
                {
                    Info.Refresh();
                    if (Info.Exists)
                    {
                        Info.Delete();
                    }

                    Info = null;
                }
            }

            Disposed = true;
        }