I have searched and only found this information for console, but I was wondering if it is possible to read text from a file on my local machine into the code, format it, and display in on screen? We have a text file with some legal jargon that can be updated periodically, and instead of having the user sift through code, we were wanting to just update the text file and have the changes apply online.
Thanks!
EDIT: Thanks to everyone’s comments, here is an edit with the requirements. The program is in C# ASP.NET website. I have read many articles about this being done in a console, but I am not sure how to make it work for me. Thanks again for everybody’s contribution.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You have the complete program (ASP.net). You must have a file inside the App_Data folder inside your ASP.net app, In this App your file name “Details.txt” which should be available inside your App_Data folder.
You have Hidden-field and a paragraph available in your web page. When form loads, at that moment read the data from the text file and populate to the Hidden-field control. And in $(document).ready() Jquery function populate data to paragraph from Hidden-field.
Your .aspx page :
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="ReadFromTextFileToTextBoxWebApp._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<style type="text/css" >
.details
{
background-color:Purple;color:yellow;top: 100px;
}
.txtDetails
{
left:150px;width:200px;height:100px;
}
</style>
<script src="Scripts/jquery-1.8.3.min.js" type="text/javascript"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function () {
var data = $("#<%=HiddenField1.ClientID %>").val();
$('#pTextData').text(data);
});
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
Welcome to ASP.NET!
</h2>
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<p id="pTextData">
</p>
</div>
</asp:Content>
and here is your code behind page :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
namespace ReadFromTextFileToTextBoxWebApp
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var data = File.ReadAllText(Server.MapPath("~/App_Data/Details.txt"));
HiddenField1.Value = data.ToString();
}
}
}
Method 2
Here are two methods to work in .Net
var legal = File.ReadAllText(@"C:LegalLegalease.txt"); // Or from the CWD of where the program is executing var legal = File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "Legalease.txt"));
Update
Remember the Asp.Net is running as a user defined in IIS’s application pool for that website. If the user does not have read access to where the file exists, it cannot be read. Make sure the user defined in the website’s application pool has the right to read the file and verify the file has been published to the read location.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0