Пример #1
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, BufferSize, 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;
        }
Пример #2
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);
            }
        }