Ejemplo n.º 1
0
        /* ReadFile should be made public and tested
         *
         * All methods of this method would be public and tested,
         * But we still would write another unit test for this method,
         * with mock of StreamReader.
         */
        public static List <string> ReadFile(string path, string fileName, FileExtensionEnum fileExt)
        {
            var fileNameWithExt = AddExtensionToFileName(fileName, fileExt);
            var fullPath        = GetFullPath(path, fileNameWithExt);
            var lines           = ReadFile(fullPath);

            return(lines);
        }
        public bool GetAccess(string dialogTitle, FileExtensionEnum fileExtension)
        {
            switch (fileExtension)
            {
            case FileExtensionEnum.XML:
                dialog.DefaultExt = "xml";
                dialog.Filter     = $"{dialog.Filter}|XML files (*.xml)|*.xml";
                break;

            case FileExtensionEnum.XSD:
                dialog.DefaultExt = "xsd";
                dialog.Filter     = $"{dialog.Filter}|XSD files (*.xsd)|*.xsd";
                break;

            default:
                break;
            }
            return(dialog.ShowDialog() == true);
        }
Ejemplo n.º 3
0
        public void Should_AddExtensionToFileName(string fileName, FileExtensionEnum extension, string expected)
        {
            var fileNameWithExt = FileReader.AddExtensionToFileName(fileName, extension);

            Assert.Equal(fileNameWithExt, expected);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// T is a object.
        /// By default file will be saved on local machine desktop.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="source"></param>
        /// <param name="path"></param>
        /// <param name="fileType"></param>
        /// <returns>Awaitable task.</returns>
        public static async System.Threading.Tasks.Task ToCsv <T>(this T source, string path = null, FileExtensionEnum fileType = FileExtensionEnum.Csv) where T : class
        {
            source.ThrowIfNull();
            System.Text.StringBuilder builder = new System.Text.StringBuilder();
            if (!string.IsNullOrEmpty(path))
            {
                builder.Append(path).Append(fileType);
            }
            else
            {
                builder.Clear()
                .Append(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)).Append('/')
                .Append(System.DateTime.Now.ToShortDateString().Replace('/', '-')).Append('.');
                builder.Append(fileType);
            }

            var propertiesInfo = source.GetType().GetProperties().Select(x => x.Name);
            var objects        = propertiesInfo as System.Collections.Generic.IList <string> ?? propertiesInfo.ToList();

            objects.ThrowIfNull();

            using (System.IO.FileStream stream = System.IO.File.Create(builder.ToString()))
            {
                // Property name write.
                builder.Clear();
                foreach (var s in objects)
                {
                    if (s == objects.LastOrDefault())
                    {
                        builder.Append(s);
                        continue;
                    }
                    builder.Append(s).Append(',');
                }

                byte[] text = new System.Text.UTF8Encoding(true).GetBytes(builder + System.Environment.NewLine);
                await stream.WriteAsync(text, 0, text.Length);

                // Property value write.
                builder.Clear();
                foreach (var s in objects)
                {
                    var value = source.GetType().GetProperty(s)?.GetValue(source, null);
                    if (s == objects.LastOrDefault())
                    {
                        builder.Append(value);
                        continue;
                    }
                    builder.Append(value).Append(',');
                }
                text = new System.Text.UTF8Encoding(true).GetBytes(builder.ToString());
                await stream.WriteAsync(text, 0, builder.Length);
            }
        }
Ejemplo n.º 5
0
 public static string AddExtensionToFileName(string fileName, FileExtensionEnum extEnum)
 {
     return(string.Format("{0}.{1}", fileName, extEnum.GetDescription()));
 }