Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading from a local text file into C#? [closed]

Tags:

c#

asp.net

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.

like image 844
Peter Avatar asked Oct 28 '13 16:10

Peter


People also ask

How to read text from a file in C?

Happy coding. You can copy paste the below C Program to read text from a file Using while fgetc () function, in c compiler to check how the source code work. After you compile and run the above c program to read text from a file and display, your C compiler asks you to enter the filename to read the text and display.

How to read text from a file and display it?

After you compile and run the above c program to read text from a file and display, your C compiler asks you to enter the filename to read the text and display. After you enter a .txt file name, the compiler will read the file and display its text.

Where is data stored in C++?

This kind of data is typically stored in files on disk. Since reading from files involves interacting with your operating system, it amounts to a rather complex task. In this article, we’ll look at C++ streams, file handling, and three different methods for reading data from a file into a C++ program.

What are C++ streams and how to read files?

In C++, the workflow of reading a file is a bit more complex — there is the added step of reading to or from a stream object. To understand how our C++ programs interact with files, let us now take a look at the concept of streams in C++. What Are C++ Streams? C++ streams are interfaces for processing sequence-like input and output.


2 Answers

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();   
        }           
    }
}
like image 112
6 revs, 3 users 98% Avatar answered Oct 05 '22 13:10

6 revs, 3 users 98%


Here are two methods to work in .Net

var legal = File.ReadAllText(@"C:\Legal\Legalease.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.

like image 21
ΩmegaMan Avatar answered Oct 05 '22 13:10

ΩmegaMan