Exemplo n.º 1
0
        private void WriteFile(AdapterMessage message, NodeParams p)
        {
            lock (this)
            {
                string filepath = Path.Combine(p.Path, p.Filename);

                FileMode fileMode = FileMode.Create;
                if (p.AppendToExisting)
                {
                    fileMode = FileMode.Append;
                }

                // Create directory if it does not exist. This enables paths like 'c:\temp\%date%'
                if (Directory.Exists(p.Path) == false)
                {
                    Directory.CreateDirectory(p.Path);
                }

                using (FileStream fs = new FileStream(filepath, fileMode, FileAccess.Write, FileShare.None))
                {
                    // Seek to the end of the file in case we're appending to existing data
                    fs.Seek(0, SeekOrigin.End);
                    if (message.IsString)
                    {
                        using (StreamWriter sw = new StreamWriter(fs))
                        {
                            sw.Write(message.GetDataAsString());
                        }
                    }
                    else
                    {
                        byte[] buffer = message.GetDataAsArray();
                        fs.Write(buffer, 0, buffer.Length);
                    }
                }


                // Preserve the creation time of source if it is configured to do so and we're not appending
                if (p.PreserveCreationTime && fileMode != FileMode.Append)
                {
                    string creationTime = message.Parameters.GetValueAsString("CreationTime");
                    if (creationTime != null)
                    {
                        try
                        {
                            DateTime time = DateTime.Parse(creationTime);
                            System.IO.File.SetCreationTime(filepath, time);
                        }
                        catch (Exception ex)
                        {
                            // Just skip if not possible
                            Tracer.WriteException(ex);
                        }
                    }
                }
            }
        }
Exemplo n.º 2
0
        public override void ReturnResponse(AdapterMessage message, object state)
        {
            try
            {
                Tracer.WriteBegin();
                if (m_NodeParams.IsTwoWay)
                {
                    m_NodeParams = NodeParams.Extract(NodeParamList);

                    string filepath = Path.Combine(m_NodeParams.ReplyPath, m_NodeParams.ReplyFilename);

                    Tracer.WriteLine($"Writing response-file: '{filepath}'");
                    System.IO.File.WriteAllBytes(filepath, message.GetDataAsArray());
                }
            }
            finally
            {
                Tracer.WriteEnd();
            }
        }