WebMarkupMin.Sample.Logic.Services.FileContentService.GetFileContent C# (CSharp) Method

GetFileContent() public method

public GetFileContent ( string filePath ) : string
filePath string
return string
		public string GetFileContent(string filePath)
		{
			if (string.IsNullOrWhiteSpace(filePath))
			{
				throw new ArgumentException(
					string.Format(CommonStrings.ErrorMessage_FilePathNotSpecified, filePath),
					"filePath"
				);
			}

			string content;
			string fullFilePath = _textContentDirectoryPath.TrimEnd('/') + "/" + filePath;
			string physicalFilePath = GetPhysicalFilePath(fullFilePath);

			try
			{
				using (FileStream fileStream = File.OpenRead(physicalFilePath))
				using (var reader = new StreamReader(fileStream))
				{
					content = reader.ReadToEnd();
				}
			}
			catch (FileNotFoundException)
			{
				throw new FileNotFoundException(
					string.Format(CommonStrings.ErrorMessage_FileNotFound, filePath));
			}
			catch (DirectoryNotFoundException)
			{
				throw new FileNotFoundException(
					string.Format(CommonStrings.ErrorMessage_FileNotFound, filePath));
			}

			return content;
		}

Usage Example

Ejemplo n.º 1
0
		protected void Page_Load(object sender, EventArgs e)
		{
			var fileContentService = new FileContentService(
				ConfigurationManager.AppSettings["webmarkupmin:Samples:TextContentDirectoryPath"]);
			Body = fileContentService.GetFileContent("change-log.html");
		}