Пример #1
0
        //--------------------------------------------------------------------------------------
        public static void ReadFile(IRaiseEvent bh, int t, FileInfo fileInfo)
        {
            try
            {
                if (fileInfo.Exists)
                {
                    FileReadWriteState ws = new FileReadWriteState(bh, t, fileInfo);
                    ws.mFileStream = new FileStream(fileInfo.FullName, FileMode.Open, FileAccess.Read);

                    IAsyncResult asyncResult = ws.mFileStream.BeginRead(
                                ws.mBuffer, 0, ws.mBuffer.Length,
                                new AsyncCallback(ReadFileComplete), ws);
                }
                else
                {
                    throw new Exception("File: " + fileInfo.FullName + " does not exist");
                }
            }
            catch (Exception e)
            {
                FileReadWriteState st = new FileReadWriteState(bh, t, fileInfo);
                st.RaiseException(e);
            }
        }
Пример #2
0
        //--------------------------------------------------------------------------------------
        public static bool WriteFile(IRaiseEvent bh, int t, FileInfo fileInfo, byte[] buffer, bool rename)
        {
            try
            {
                if (fileInfo.Exists)
                {
                    if (rename)
                    {
                        DateTime td = DateTime.Now;
                        int index = fileInfo.FullName.LastIndexOf('.');
                        string newName = fileInfo.FullName.Remove(index, fileInfo.FullName.Length - index);
                        newName = newName + td.ToString("MMddHHmmss") + ".txt";
                        fileInfo.CopyTo(newName);
                    }
                    else
                    {
                        fileInfo.Delete();
                    }
                }

                FileReadWriteState ws = new FileReadWriteState(bh, t, fileInfo);
                ws.mFileStream = new FileStream(fileInfo.FullName, FileMode.Create, FileAccess.Write, FileShare.None, BUFFER_SIZE, true);

                IAsyncResult asyncResult = ws.mFileStream.BeginWrite(
                        buffer, 0, buffer.Length,
                        new AsyncCallback(WriteFileComplete), ws);
            }
            catch (Exception e)
            {
                FileReadWriteState st = new FileReadWriteState(bh, t, fileInfo);
                st.RaiseException(e);
            }
               return true;
        }