从本地文本文件读取到 C#?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19640590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Reading from a local text file into C#?
提问by Peter
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.
编辑:感谢大家的意见,这里是一个编辑的要求。该程序位于 C# ASP.NET 网站中。我已经阅读了很多关于在控制台中完成此操作的文章,但我不确定如何使其对我有用。再次感谢大家的贡献。
采纳答案by Chandan Kumar
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.
您拥有完整的程序 (ASP.net)。您App_Data
的 ASP.net 应用程序内的文件夹中必须有一个文件,在此应用程序中,您的文件名“Details.txt”应该在您的App_Data
文件夹中可用。
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.
您的网页中有 Hidden-field 和一个段落可用。当表单加载时,此时从文本文件中读取数据并填充到隐藏字段控件。并在$(document).ready()
Jquery 函数中将 数据从隐藏字段填充到段落。
Your .aspx
page :
您的.aspx
页面:
<%@ 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();
}
}
}
回答by ΩmegaMan
Here are two methods to work in .Net
这里有两种在 .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.
请记住,Asp.Net 以在该网站的IIS 应用程序池中定义的用户身份运行。如果用户对文件所在的位置没有读取权限,则无法读取该文件。确保网站应用程序池中定义的用户有权读取文件并验证文件已发布到读取位置。