Exemple #1
0
        public static string Test06()
        {
            System.IO.FileMode a = System.IO.FileMode.Create;

            Console.WriteLine("a=" + a);
            return(a.ToString());
        }
Exemple #2
0
        private static uint GetMode(IO.FileMode mode)
        {
            uint umode = 0;

            switch (mode)
            {
            case IO.FileMode.CreateNew:
                umode = PathInternal.DllImport.CREATE_NEW;
                break;

            case IO.FileMode.Create:
                umode = PathInternal.DllImport.CREATE_ALWAYS;
                break;

            case IO.FileMode.Append:
                umode = PathInternal.DllImport.OPEN_ALWAYS;
                break;

            case IO.FileMode.Open:
                umode = PathInternal.DllImport.OPEN_EXISTING;
                break;

            case IO.FileMode.OpenOrCreate:
                umode = PathInternal.DllImport.OPEN_ALWAYS;
                break;

            case IO.FileMode.Truncate:
                umode = PathInternal.DllImport.TRUNCATE_EXISTING;
                break;
            }
            return(umode);
        }
Exemple #3
0
        public static IO.FileStream Open(string filepath, IO.FileMode mode, IO.FileAccess access, IO.FileShare share)
        {
            //opened in the specified mode , access and  share
            IO.FileStream fs      = null;
            uint          umode   = GetMode(mode);
            uint          uaccess = GetAccess(access);
            uint          ushare  = GetShare(share);

            if (mode == IO.FileMode.Append)
            {
                uaccess = PathInternal.DllImport.FILE_APPEND_DATA;
            }

            SafeFileHandle sh     = PathInternal.DllImport.CreateFileW(PathInternal.ConvertToUnicodePath(filepath), uaccess, ushare, IntPtr.Zero, umode, PathInternal.DllImport.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero);
            int            iError = Marshal.GetLastWin32Error();

            if ((iError > 0 && !(mode == IO.FileMode.Append && iError != PathInternal.DllImport.ERROR_ALREADY_EXISTS)) || sh.IsInvalid)
            {
                throw new Exception("Error opening file Win32 Error:" + iError);
            }
            else
            {
                fs = new IO.FileStream(sh, access);
            }
            // if opened in append mode
            if (mode == IO.FileMode.Append)
            {
                if (!sh.IsInvalid)
                {
                    PathInternal.DllImport.SetFilePointer(sh, 0, IntPtr.Zero, PathInternal.DllImport.FILE_END);
                }
            }
            return(fs);
        }
Exemple #4
0
        /// <summary>
        /// 获取附件数据流类
        /// </summary>
        /// <param name="attachUrl">附件路径</param>
        /// <returns></returns>
        public static void FileInsert(List <byte[]> fileContextList, string attachUrl)
        {
            if (fileContextList != null && fileContextList.Count > 0)
            {
                string physicalpath = Funs.RootPath;
                //HttpContext.Current.Request.PhysicalApplicationPath;
                string fullPath = physicalpath + attachUrl;
                if (!File.Exists(fullPath))
                {
                    byte[] fileContext = fileContextList[0];
                    int    index       = fullPath.LastIndexOf("\\");
                    string filePath    = fullPath.Substring(0, index);
                    if (!Directory.Exists(filePath))
                    {
                        Directory.CreateDirectory(filePath);
                    }
                    //string savePath = fullPath + fileName;

                    //文件读写模式
                    System.IO.FileMode fileMode = System.IO.FileMode.Create;
                    //写入文件
                    using (System.IO.FileStream fs = new System.IO.FileStream(fullPath, fileMode, System.IO.FileAccess.Write))
                    {
                        fs.Write(fileContext, 0, fileContext.Length);
                    }
                }
            }
        }
Exemple #5
0
        public static void LogError(Exception ex)
        {
            try {
                if (logFilePath == null)
                {
                    return;
                }

                // checks if the log is bigger than 1MB, and if so, overwrite it
                System.IO.FileMode mode = FileMode.Append;
                FileInfo           info = new FileInfo(logFilePath);
                if (info.Length > 900000)
                {
                    mode = FileMode.Create;
                }


                System.IO.FileStream fstream = new FileStream(logFilePath, mode);

                StreamWriter writer = new StreamWriter(fstream);
                writer.WriteLine(DateTime.Now);
                writer.WriteLine(ex.Message);
                writer.WriteLine(ex.StackTrace.ToString());
                writer.WriteLine("------------------------------------------------------------------------------");
                writer.Close();
            }
            catch (Exception) { }
        }
Exemple #6
0
    //********************************************************************************************
    //
    //********************************************************************************************

    public static bool Serialize(T instance, string filename, System.IO.FileMode fmode, SERIALIZATION.MODE smode)
    {
        if (m_formatter == null)
        {
            return(false);
        }

        if (instance == null)
        {
            return(false);
        }

        if (string.IsNullOrEmpty(filename))
        {
            return(false);
        }

        Stream stream = System.IO.File.Open(filename, fmode);

        bool result = Serialize(instance, stream, smode);

        if (stream != null)
        {
            stream.Close();
        }

        return(result);
    }
Exemple #7
0
 static extern SafeFileHandle CreateFile(String fileName,
                                         int desiredAccess,
                                         System.IO.FileShare shareMode,
                                         IntPtr securityAttrs,
                                         System.IO.FileMode creationDisposition,
                                         int flagsAndAttributes,
                                         IntPtr templateFile);
        public FileStream GetFileStream(string fileName, System.IO.FileMode fileMode)
        {
            IsolatedStorageFile       isoStore  = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
            IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, fileMode, isoStore);

            return(isoStream);
        }
Exemple #9
0
 public void Create(string name, byte[] data, System.IO.FileMode mode = FileMode.Create)
 {
     using (FileStream fs = new FileStream(working_dir + name, FileMode.Create | FileMode.Append))
     {
         fs.Write(data, 0, data.Length);
     }
 }
Exemple #10
0
 /// <summary>
 /// Create a new filestream. Can be synced over async task (netstandard)
 /// </summary>
 private FileStream CreateFileStream(string path, System.IO.FileMode mode, FileAccess access, FileShare share)
 {
     return(System.Threading.Tasks.Task.Run(() => new FileStream(path, mode, access, share, BasePage.PAGE_SIZE))
            .ConfigureAwait(false)
            .GetAwaiter()
            .GetResult());
 }
Exemple #11
0
        public NTStream_(string fName, System.IO.FileMode mode)
        {
            fileName = fName;
            if (mode == System.IO.FileMode.Open)
            {
                bs = new BackupFileStream(fName,
                                          Alphaleonis.Win32.Filesystem.FileMode.Open,
                                          /* FileSystemRights.ReadPermissions|FileSystemRights.SystemSecurity, */
                                          FileSystemRights.ReadPermissions,
                                          Alphaleonis.Win32.Filesystem.FileShare.ReadWrite,
                                          Alphaleonis.Win32.Filesystem.FileOptions.SequentialScan |
                                          Alphaleonis.Win32.Filesystem.FileOptions.BackupSemantics

                                          /*|Alphaleonis.Win32.Filesystem.FileOptions.NoBuffering
                                           |Alphaleonis.Win32.Filesystem.FileOptions.BackupSemantics*/

                                          );
            }
            else if (mode == System.IO.FileMode.OpenOrCreate)
            {
                bs = new BackupFileStream(fName,
                                          Alphaleonis.Win32.Filesystem.FileMode.OpenOrCreate,                /*also use TRUNCATE??*/
                                          FileSystemRights.AppendData | FileSystemRights.CreateFiles
                                          | FileSystemRights.Write | FileSystemRights.SystemSecurity,
                                          Alphaleonis.Win32.Filesystem.FileShare.Read,
                                          Alphaleonis.Win32.Filesystem.FileOptions.SequentialScan
                                          );
            }
            //bs = new BackupFileStream(fName, Alphaleonis.Win32.Filesystem.FileMode.Open);
            //Console.WriteLine ("bstream length="+bs.Length);
        }
Exemple #12
0
        public Stream OpenFileStream(string key, string filename, System.IO.FileMode mode, System.IO.FileAccess access)
        {
            switch (access)
            {
            case FileAccess.Read:
                return(GetFileData(key, filename));

            case FileAccess.Write:
                switch (mode)
                {
                case FileMode.Open:
                    throw new FileNotFoundException();

                case FileMode.Append:
                    throw new InvalidOperationException();

                default:
                    var ms = new FileCacheDatabaseWriteStream();
                    ms.HashKey  = key;
                    ms.Filename = filename;
                    ms.Cache    = this;
                    return(ms);
                }

            default:
                throw new InvalidOperationException();
            }
            throw new NotImplementedException();
        }
Exemple #13
0
        public int CreateFile(
            string filename,
            System.IO.FileAccess access,
            System.IO.FileShare share,
            System.IO.FileMode mode,
            System.IO.FileOptions options,
            DokanFileInfo info)
        {
            Trace.WriteLine(string.Format("CreateFile {0}", filename));

            if (mode == FileMode.CreateNew)
            {
                var blob = this.GetBlob(filename, false);
                blobsWriting.TryAdd(filename, blob.OpenWrite());

                return(0);
            }

            if (GetFileInformation(filename, new FileInformation(), new DokanFileInfo(0)) == 0)
            {
                return(0);
            }
            else
            {
                return(-DokanNet.ERROR_FILE_NOT_FOUND);
            }
        }
 /// <summary>
 /// Writes content to the file
 /// </summary>
 /// <param name="Content">Content to write</param>
 /// <param name="Mode">Mode to open the file as</param>
 /// <returns>The result of the write or original content</returns>
 public byte[] Write(byte[] Content, System.IO.FileMode Mode = FileMode.Create)
 {
     if (InternalFile == null)
     {
         return(Content);
     }
     return(InternalFile.Write(Content, Mode));
 }
 /// <summary>
 /// Writes content to the file
 /// </summary>
 /// <param name="Content">Content to write</param>
 /// <param name="Mode">Mode to open the file as</param>
 /// <param name="Encoding">Encoding to use for the content</param>
 /// <returns>The result of the write or original content</returns>
 public string Write(string Content, System.IO.FileMode Mode = FileMode.Create, Encoding Encoding = null)
 {
     if (InternalFile == null)
     {
         return(Content);
     }
     return(InternalFile.Write(Content, Mode, Encoding));
 }
Exemple #16
0
 private static extern SafeFileHandle CreateFilePrivate(
     string lpFileName,
     int dwDesiredAccess,
     System.IO.FileShare dwShareMode,
     ref SECURITY_ATTRIBUTES securityAttrs,
     System.IO.FileMode dwCreationDisposition,
     int dwFlagsAndAttributes,
     IntPtr hTemplateFile);
Exemple #17
0
        public File OpenFile(string path, FileMode mode, FileAccess access)
        {
            // Resolve the OpenOrCreate file mode.
            if (mode == FileMode.OpenOrCreate)
            {
                mode = FileExists(path) ? FileMode.Open : FileMode.Create;
            }

            // Convert the file mode and access to the System.IO types.
            IO.FileMode   fileMode   = ConvertMode(mode);
            IO.FileAccess fileAccess = ConvertAccess(access);

            // Attempt to open the stream.
            IO.Stream stream;

            try
            {
                stream = new IO.FileStream(path, fileMode, fileAccess);
            }
            catch (NotSupportedException)
            {
                throw new IOException("Path refers to a non-file device");
            }
            catch (IO.FileNotFoundException)
            {
                throw new FileNotFoundException("File not found");
            }
            catch (IO.DirectoryNotFoundException)
            {
                throw new DirectoryNotFoundException("Invalid path");
            }
            catch (IO.IOException)
            {
                throw new IOException("Generic IO exception");
            }
            catch (System.Security.SecurityException)
            {
                throw new SecurityException("No permission");
            }
            catch (UnauthorizedAccessException)
            {
                throw new SecurityException("Requested access denied");
            }

            // Build the file info.
            FileInfo fileInfo;

            if (mode == FileMode.Open || (mode == FileMode.Append && FileExists(path)))
            {
                fileInfo = GetFileInfo(path);
            }
            else
            {
                fileInfo = CreateFileInfo(path);
            }

            return(new File(fileInfo, stream));
        }
Exemple #18
0
 public IOStream(string[] filePaths, System.IO.FileMode mode)
     : base()
 {
     Streams = new FileStream[filePaths.Length];
     for (int i = 0; i < Streams.Length; i++)
     {
         Streams[i] = new FileStream(filePaths[i], mode);
     }
 }
Exemple #19
0
        public Stream OpenFile(string filePath, System.IO.FileMode fileMode)
        {
            if (filePath == null || filePath.Trim().Length == 0)
            {
                throw new ArgumentNullException("File path arguments are invalid.");
            }

            return(new FileStream(Path.Combine(_path, filePath), fileMode, System.IO.FileAccess.ReadWrite, System.IO.FileShare.Read));
        }
Exemple #20
0
        /// <summary>
        /// Not used
        /// </summary>
        /// <param name="Content">Not used</param>
        /// <param name="Mode">Not used</param>
        /// <returns>The result of the write or original content</returns>
        public override byte[] Write(byte[] Content, System.IO.FileMode Mode = FileMode.Create)
        {
            var Request = WebRequest.Create(InternalFile) as FtpWebRequest;

            Request.Method = WebRequestMethods.Ftp.UploadFile;
            SetupData(Request, Content);
            SetupCredentials(Request);
            return(SendRequest(Request).ToByteArray());
        }
        /// <summary>
        /// Constructor
        /// </summary>
        public CaptureFileWriterDevice(string captureFilename, System.IO.FileMode mode = FileMode.OpenOrCreate)
        {
            m_pcapFile = captureFilename;

            // append isn't possible without some difficulty and not implemented yet
            if (mode == FileMode.Append)
            {
                throw new InvalidOperationException("FileMode.Append is not supported, please contact the developers if you are interested in helping to implementing it");
            }
        }
Exemple #22
0
 internal static unsafe SafeFileHandle CreateFile2(
     string lpFileName,
     int dwDesiredAccess,
     System.IO.FileShare dwShareMode,
     System.IO.FileMode dwCreationDisposition,
     CREATEFILE2_EXTENDED_PARAMETERS *pCreateExParams)
 {
     lpFileName = PathInternal.EnsureExtendedPrefixOverMaxPath(lpFileName);
     return(CreateFile2Private(lpFileName, dwDesiredAccess, dwShareMode, dwCreationDisposition, pCreateExParams));
 }
Exemple #23
0
        public FATXFileStream(string Path, System.IO.FileMode fmode, File file)
        {
            xFile = file;
            // Set our position
            long off = VariousFunctions.GetBlockOffset(xFile.BlocksOccupied[0], xFile);

            Underlying = new FileStream(Path, fmode);
            //Underlying.Position = off;
            Position = 0;
        }
Exemple #24
0
        /// <summary>
        /// Initializes a new instance of the <see cref="IsolatedStorageFileStream"/> class giving access to the file designated by path,
        /// in the specified mode, with the specified file access, using the file sharing mode specified by share,
        /// and in the context of the <see cref="IsolatedStorageFile"/> specified by isf.
        /// </summary>
        /// <param name="path">The relative path of the file within isolated storage.</param>
        /// <param name="mode">One of the <see cref="FileMode"/> values.</param>
        /// <param name="access">A bitwise combination of the <see cref="FileAccess"/> values.</param>
        /// <param name="share">A bitwise combination of the <see cref="FileShare"/> values.</param>
        /// <param name="isf">The <see cref="IsolatedStorageFile"/> in which to open the <see cref="IsolatedStorageFileStream"/>.</param>
        public IsolatedStorageFileStream(string path, System.IO.FileMode mode, System.IO.FileAccess access,
                                         System.IO.FileShare share, IsolatedStorage.IsolatedStorageFile isf)
            : base(Path.Combine(isf.BasePath, path), mode, access, share)
        {
            //also set the hidden attribute (just in case)
            System.IO.FileInfo fi = new System.IO.FileInfo(System.IO.Path.Combine(isf.BasePath, path));
            fi.Attributes |= System.IO.FileAttributes.Hidden;

            _isolatedStorageFile = isf;
        }
Exemple #25
0
        /// <summary>
        /// Create a new filestream. Can be synced over async task (netstandard)
        /// </summary>
        private FileStream CreateFileStream(string path, System.IO.FileMode mode, FileAccess access, FileShare share)
        {
#if !NET35
            if (_options.Async)
            {
                return(this.SyncOverAsync(() => new FileStream(path, mode, access, share, BasePage.PAGE_SIZE)));
            }
#endif
            return(new FileStream(path, mode, access, share, BasePage.PAGE_SIZE));
        }
Exemple #26
0
        protected override FileSystemFileStream ImplOpenFile(string FileName, System.IO.FileMode FileMode)
        {
            switch (FileMode)
            {
            case FileMode.Open:
                return(base.ImplOpenFile(FileName, FileMode));

            default:
                throw(new NotImplementedException());
            }
        }
Exemple #27
0
 private static FileMode GetFileMode(IO.FileMode mode)
 {
     if (mode != IO.FileMode.Append)
     {
         return((FileMode)(int)mode);
     }
     else
     {
         return((FileMode)(int)IO.FileMode.OpenOrCreate);
     }
 }
Exemple #28
0
 public static void Write(string fileName, string text, System.IO.FileMode fileMode)
 {
     Directory.CreateDirectory(Path.GetDirectoryName(fileName));
     using (FileStream file = new FileStream(fileName, fileMode, FileAccess.Write, FileShare.Read))
     {
         using (StreamWriter writer = new StreamWriter(file, Encoding.Unicode))
         {
             writer.WriteLine(text);
         }
     }
 }
Exemple #29
0
 public Stream TryCreateFileStream(string path, System.IO.FileMode mode)
 {
     try
     {
         return(new FileStream(path, mode));
     }
     catch (Exception)
     {
         return(null);
     }
 }
 // Translate managed FileMode member to unmanaged file mode flag.
 internal static WindowsNative.FileMode TranslateFileMode(System.IO.FileMode mode)
 {
     if (mode != System.IO.FileMode.Append)
     {
         return((WindowsNative.FileMode)(int) mode);
     }
     else
     {
         return((WindowsNative.FileMode)(int) System.IO.FileMode.OpenOrCreate);
     }
 }
Exemple #31
0
 public StreamOutput(string f, System.IO.FileMode fm)
     : base(OutputType.Stream)
 {
     filename = f; filemode = fm;
 }