示例#1
0
文件: FileIO.cs 项目: dcuccia/VTS
        ///// <summary>
        ///// Writes data of a specified type to an XML file
        ///// </summary>
        ///// <typeparam name="T">Type of the data to be written</typeparam>
        ///// <param name="myObject">Object to be written</param>
        ///// <param name="filename">Name of the XML file to write</param>
        //public static void WriteToXML<T>(this T myObject, string filename)
        //{
        //    using (Stream stream = StreamFinder.GetFileStream(filename, FileMode.Create))
        //    {
        //        //new DataContractSerializer(typeof(T)).WriteObject(stream, myObject);
        //        myObject.WriteToXMLStream(stream);
        //    }
        //}

        /// <summary>
        /// Writes data of a specified type to an JSON file
        /// </summary>
        /// <typeparam name="T">Type of the data to be written</typeparam>
        /// <param name="myObject">Object to be written</param>
        /// <param name="filename">Name of the JSON file to write</param>
        public static void WriteToJson <T>(this T myObject, string filename)
        {
            using (Stream stream = StreamFinder.GetFileStream(filename, FileMode.Create))
            {
                //new DataContractSerializer(typeof(T)).WriteObject(stream, myObject);
                myObject.WriteJsonToStream(stream);
            }
        }
示例#2
0
 /// <summary>
 /// Reads data of a specified type from an XML file
 /// </summary>
 /// <typeparam name="T">Type of the data</typeparam>
 /// <param name="filename">Name of the XML file to be read</param>
 /// <returns>The data as the specified type</returns>
 public static T ReadFromXML <T>(string filename)
 {
     using (Stream stream = StreamFinder.GetFileStream(filename, FileMode.Open))
     {
         return(ReadFromStream <T>(stream));
         //return (T)new DataContractSerializer(typeof(T)).ReadObject(stream);
     }
 }
示例#3
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 /// Copy a file from resources to an external location
 /// </summary>
 /// <example>FileIO.CopyFileFromResources("Resources/resourcesfile.txt", Path.Combine(resultsFolder, "resourcefile.txt"), "Vts.Desktop.Test");</example>
 /// <param name="sourceFileName">Path and filename of the file in resources</param>
 /// <param name="destinationFileName">Path and filename of the destination location</param>
 /// <param name="projectName">The name of the project where the file in resources is located</param>
 public static void CopyFileFromResources(string sourceFileName, string destinationFileName, string projectName)
 {
     using (var stream = StreamFinder.GetFileStreamFromResources(sourceFileName, projectName))
     {
         var emptyStream = StreamFinder.GetFileStream(destinationFileName, FileMode.Create);
         stream.CopyTo(emptyStream);
         emptyStream.Close();
     }
 }
示例#4
0
文件: FileIO.cs 项目: dcuccia/VTS
        /// <summary>
        /// Writes the string to a text file
        /// </summary>
        /// <param name="text">Text to write to the file</param>
        /// <param name="filename">Name of the text file to write </param>
        public static void WriteToTextFile(string text, string filename)
        {
            Stream stream = StreamFinder.GetFileStream(filename, FileMode.Create);

            using (StreamWriter outfile = new StreamWriter(stream))
            {
                outfile.Write(text);
            }
        }
示例#5
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="fileName">Name of the binary file to read</param>
 /// <param name="readerMap"></param>
 /// <returns></returns>
 public static IEnumerable <T> ReadFromBinaryCustom <T>(string fileName, Func <BinaryReader, T> readerMap)
 {
     using (Stream s = StreamFinder.GetFileStream(fileName, FileMode.Open))
     {
         foreach (var item in ReadStreamFromBinaryCustom <T>(s, readerMap))
         {
             yield return(item);
         }
     }
 }
示例#6
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 /// Reads array from a binary file using explicitly-set dimensions
 /// </summary>
 /// <typeparam name="T">Type of the array being read</typeparam>
 /// <param name="filename">Name of the file from which to read the array</param>
 /// <param name="dims">Dimensions of the array</param>
 /// <returns>Array from the file</returns>
 public static Array ReadArrayFromBinary <T>(string filename, params int[] dims) where T : struct
 {
     using (Stream s = StreamFinder.GetFileStream(filename, FileMode.Open))
     {
         using (BinaryReader br = new BinaryReader(s))
         {
             return(new ArrayCustomBinaryReader <T>(dims).ReadFromBinary(br));
         }
     }
 }
        public static T ReadFromJsonFile <T>(string filename)
        {
            using (var stream = StreamFinder.GetFileStream(filename, FileMode.Open))
                using (var sr = new StreamReader(stream, false))
                {
                    var json = sr.ReadToEnd();

                    return(ReadFromJson <T>(json));
                }
        }
示例#8
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 ///
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="fileName">Name of the binary file to read</param>
 /// <param name="projectName">Project name where resources is located</param>
 /// <param name="readerMap"></param>
 /// <returns></returns>
 public static IEnumerable <T> ReadFromBinaryInResourcesCustom <T>(string fileName, string projectName, Func <BinaryReader, T> readerMap)
 {
     using (Stream s = StreamFinder.GetFileStreamFromResources(fileName, projectName))
     {
         foreach (var item in ReadStreamFromBinaryCustom <T>(s, readerMap))
         {
             yield return(item);
         }
     }
 }
        public static void WriteToJsonFile <T>(this T myObject, string filename)
        {
            var settingsJson = WriteToJson(myObject);

            using (var stream = StreamFinder.GetFileStream(filename, FileMode.Create))
                using (var sw = new StreamWriter(stream))
                {
                    sw.Write(settingsJson);
                }
        }
示例#10
0
文件: FileIO.cs 项目: dcuccia/VTS
        //#region Write/ReadArrayToBinary Helpers

        //private static void WriteArrayToBinaryInternal<T>(BinaryWriter bw, IEnumerable<T> array) where T : struct
        //{
        //    if (array is IEnumerable<float>)
        //    {
        //        (array as IEnumerable<float>).ForEach(bw.Write);
        //    }
        //    else if (array is IEnumerable<double>)
        //    {
        //        (array as IEnumerable<double>).ForEach(bw.Write);
        //    }
        //    else if (array is IEnumerable<ushort>)
        //    {
        //        (array as IEnumerable<ushort>).ForEach(bw.Write);
        //    }
        //    else if (array is IEnumerable<byte>)
        //    {
        //        (array as IEnumerable<byte>).ForEach(bw.Write);
        //    }
        //}

        //private static void ReadArrayFromBinaryInternal<T>(BinaryReader br, ref Array myArray) where T : struct
        //{
        //    var dataType = typeof (T);

        //    if (dataType == typeof(double))
        //    {
        //        myArray.PopulateFromEnumerable(ReadDoubles(br, myArray.Length));
        //    }
        //    else if (dataType == typeof(float))
        //    {
        //        myArray.PopulateFromEnumerable(ReadFloats(br, myArray.Length));
        //    }
        //    else if (dataType == typeof(ushort))
        //    {
        //        myArray.PopulateFromEnumerable(ReadUShorts(br, myArray.Length));
        //    }
        //    else if (dataType == typeof(byte))
        //    {
        //        myArray.PopulateFromEnumerable(ReadBytes(br, myArray.Length));
        //    }
        //}

        //private static IEnumerable<double> ReadDoubles(BinaryReader br, int numberOfElements)
        //{
        //    for (int i = 0; i < numberOfElements; i++)
        //    {
        //        yield return br.ReadDouble();
        //    }
        //}
        //private static IEnumerable<float> ReadFloats(BinaryReader br, int numberOfElements)
        //{
        //    for (int i = 0; i < numberOfElements; i++)
        //    {
        //        yield return br.ReadSingle();
        //    }
        //}
        //private static IEnumerable<ushort> ReadUShorts(BinaryReader br, int numberOfElements)
        //{
        //    for (int i = 0; i < numberOfElements; i++)
        //    {
        //        yield return br.ReadUInt16();
        //    }
        //}
        //private static IEnumerable<byte> ReadBytes(BinaryReader br, int numberOfElements)
        //{
        //    return br.ReadBytes(numberOfElements);
        //    //for (int i = 0; i < numberOfElements; i++)
        //    //{
        //    //    yield return br.re.ReadByte();
        //    //}
        //}

        //#endregion

        /// <summary>
        ///
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="fileName">Name of the binary file to write</param>
        /// <param name="writerMap"></param>
        public static void WriteToBinaryCustom <T>(this IEnumerable <T> data, string fileName, Action <BinaryWriter, T> writerMap)
        {
            // todo: convert to "push" method with System.Observable in Rx Extensions (write upon appearance of new datum)
            using (Stream s = StreamFinder.GetFileStream(fileName, FileMode.Create))
            {
                using (BinaryWriter bw = new BinaryWriter(s))
                {
                    data.ForEach(d => writerMap(bw, d));
                }
            }
        }
示例#11
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 /// Reads a scalar value from a binary file
 /// </summary>
 /// <typeparam name="T">Type of data to be read</typeparam>
 /// <param name="filename">Name of the binary file</param>
 /// <param name="readMap"></param>
 /// <returns></returns>
 public static T ReadScalarValueFromBinary <T>(string filename, Func <BinaryReader, T> readMap)
 {
     // Create a file to write binary data
     using (Stream s = StreamFinder.GetFileStream(filename, FileMode.OpenOrCreate))
     {
         using (BinaryReader br = new BinaryReader(s))
         {
             return(readMap(br));
         }
     }
 }
示例#12
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 /// Writes a scalar value to a binary file
 /// </summary>
 /// <typeparam name="T">Type of the data to be written</typeparam>
 /// <param name="dataIN">Data to be written</param>
 /// <param name="filename">Name of the binary file to write</param>
 /// <param name="writeMap"></param>
 public static void WriteScalarValueToBinary <T>(T dataIN, string filename, Action <BinaryWriter, T> writeMap)
 {
     // Create a file to write binary data
     using (Stream s = StreamFinder.GetFileStream(filename, FileMode.OpenOrCreate))
     {
         using (BinaryWriter bw = new BinaryWriter(s))
         {
             writeMap(bw, dataIN);
         }
     }
 }
示例#13
0
文件: FileIO.cs 项目: dcuccia/VTS
        /// <summary>
        /// Reads array from a binary file in resources using explicitly-set dimensions
        /// </summary>
        /// <typeparam name="T">Type of the array being read</typeparam>
        /// <param name="filename">Name of the JSON (text) file containing the meta data</param>
        /// <param name="projectname">Project name for the location of resources</param>
        /// <param name="dims">Dimensions of the array</param>
        /// <returns>Array from the file</returns>
        public static Array ReadArrayFromBinaryInResources <T>(string filename, string projectname, params int[] dims) where T : struct
        {
            using (Stream stream = StreamFinder.GetFileStreamFromResources(filename, projectname))
            {
                using (BinaryReader br = new BinaryReader(stream))
                {
                    // Initialize the array
                    //Array dataOut = Array.CreateInstance(typeof(T), dims);
                    // Fill with data
                    //ReadArrayFromBinaryInternal<T>(br, ref dataOut);

                    return(new ArrayCustomBinaryReader <T>(dims).ReadFromBinary(br));
                }
            }
        }
示例#14
0
        /// <summary>
        /// Reads array from a binary file using explicitly-set dimensions
        /// </summary>
        /// <typeparam name="T">Type of the array being read</typeparam>
        /// <param name="filename">Name of the file from which to read the array</param>
        /// <param name="dims">Dimensions of the array</param>
        /// <returns>Array from the file</returns>
        public static Array ReadArrayFromBinary <T>(string filename, params int[] dims) where T : struct
        {
            using (Stream s = StreamFinder.GetFileStream(filename, FileMode.Open))
            {
                using (BinaryReader br = new BinaryReader(s))
                {
                    //Array dataOut = Array.CreateInstance(typeof(Time), dims);

                    return(new ArrayCustomBinaryReader <T>(dims).ReadFromBinary(br));
                    //ReadArrayFromBinaryInternal<Time>(br, ref dataOut);

                    //return dataOut;
                }
            }
        }
示例#15
0
文件: FileIO.cs 项目: dcuccia/VTS
        /// <summary>
        /// Copy a file from embedded resources in the project assembly and
        /// copies to an external location
        /// </summary>
        /// <param name="sourceFileName">Path and filename of the file in resources</param>
        /// <param name="destinationFileName">Path and filename of the destination location</param>
        /// <param name="projectName">The name of the project where the file is located</param>
        public static void CopyFileFromEmbeddedResources(string sourceFileName, string destinationFileName, string projectName)
        {
            var    currentAssembly = Assembly.Load(projectName);
            Stream emptyStream;
            Stream stream;

            using (stream = currentAssembly.GetManifestResourceStream(sourceFileName))
            {
                emptyStream = StreamFinder.GetFileStream(destinationFileName, FileMode.Create);
                if (stream != null)
                {
                    stream.CopyTo(emptyStream);
                }
            }
            emptyStream.Close();
        }
示例#16
0
文件: FileIO.cs 项目: dcuccia/VTS
 /// <summary>
 /// Writes an array to a binary file and optionally accompanying .xml file
 /// (to store array dimensions) if includeMetaData = true
 /// </summary>
 /// <param name="dataIN">Array to be written</param>
 /// <param name="filename">Name of the file where the data is written</param>
 /// <param name="includeMetaData">Boolean to determine whether to include meta data, if set to true, an accompanying XML file will be created with the same name</param>
 public static void WriteArrayToBinary(Array dataIN, string filename, bool includeMetaData)
 {
     // Write XML file to describe the contents of the binary file
     if (includeMetaData)
     {
         new MetaData(dataIN).WriteToJson(filename + ".txt");
     }
     // Create a file to write binary data
     using (Stream s = StreamFinder.GetFileStream(filename, FileMode.OpenOrCreate))
     {
         using (BinaryWriter bw = new BinaryWriter(s))
         {
             new ArrayCustomBinaryWriter().WriteToBinary(bw, dataIN);
             //WriteArrayToBinaryInternal(bw, dataIN.ToEnumerable<T>());
         }
     }
 }
示例#17
0
        /// <summary>
        /// Opens the filestream for subsequent calls to WriteDataPoint or WriteDataPoints
        /// </summary>
        private void OpenStream()
        {
            try
            {
                _stream       = StreamFinder.GetFileStream(_filename, FileMode.Create);
                _binaryWriter = new BinaryWriter(_stream);

                IsOpen = true;

                if (PreWriteAction != null)
                {
                    PreWriteAction();
                }
            }
            catch (IOException)
            {
                Close();
            }
        }