Browse Source

Add ReadAllLinesAsync method.

pull/3421/head
Alper Ebicoglu 6 years ago
parent
commit
bc547cd05a
  1. 24
      framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/ConnectionStringChangeStep.cs
  2. 48
      framework/src/Volo.Abp.Core/Volo/Abp/IO/FileHelper.cs

24
framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/ProjectBuilding/Building/Steps/ConnectionStringChangeStep.cs

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
@ -21,7 +23,8 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
{
try
{
var jsonObject = JObject.Parse(appSettingsJson.Content);
var appSettingJsonContentWithoutBom = GetStringWithoutBom(appSettingsJson.Bytes);
var jsonObject = JObject.Parse(appSettingJsonContentWithoutBom);
var connectionStringContainer = (JContainer)jsonObject["ConnectionStrings"];
var firstConnectionString = connectionStringContainer.First;
var defaultConnectionString = firstConnectionString.ToString();
@ -34,5 +37,24 @@ namespace Volo.Abp.Cli.ProjectBuilding.Building.Steps
}
}
}
private static string GetStringWithoutBom(byte[] bytes)
{
if (bytes == null)
{
return null;
}
var hasBom = bytes.Length >= 3 && bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF;
if (hasBom)
{
return Encoding.UTF8.GetString(bytes, 3, bytes.Length - 3);
}
else
{
return Encoding.UTF8.GetString(bytes);
}
}
}
}

48
framework/src/Volo.Abp.Core/Volo/Abp/IO/FileHelper.cs

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading.Tasks;
@ -73,6 +74,51 @@ namespace Volo.Abp.IO
}
}
//TODO: ReadAllLinesAsync
/// <summary>
/// Opens a text file, reads all lines of the file, and then closes the file.
/// </summary>
/// <param name="path">The file to open for reading.</param>
/// <param name="encoding">Encoding of the file. Default is UTF8</param>
/// <param name="fileMode">Specifies how the operating system should open a file. Default is Open</param>
/// <param name="fileAccess">Defines constants for read, write, or read/write access to a file. Default is Read</param>
/// <param name="fileShare">Contains constants for controlling the kind of access other FileStream objects can have to the same file. Default is Read</param>
/// <param name="bufferSize">Length of StreamReader buffer. Default is 4096.</param>
/// <param name="fileOptions">Indicates FileStream options. Default is Asynchronous (The file is to be used for asynchronous reading.) and SequentialScan (The file is to be accessed sequentially from beginning to end.) </param>
/// <returns>A string containing all lines of the file.</returns>
public static async Task<string[]> ReadAllLinesAsync(string path,
Encoding encoding = null,
FileMode fileMode = FileMode.Open,
FileAccess fileAccess = FileAccess.Read,
FileShare fileShare = FileShare.Read,
int bufferSize = 4096,
FileOptions fileOptions = FileOptions.Asynchronous | FileOptions.SequentialScan)
{
if (encoding == null)
{
encoding = Encoding.UTF8;
}
var lines = new List<string>();
using (var stream = new FileStream(
path,
fileMode,
fileAccess,
fileShare,
bufferSize,
fileOptions))
{
using (var reader = new StreamReader(stream, encoding))
{
string line;
while ((line = await reader.ReadLineAsync()) != null)
{
lines.Add(line);
}
}
}
return lines.ToArray();
}
}
}
Loading…
Cancel
Save