简体   繁体   中英

How to use text file reading/writing in server side of ASP.net web page?

I'm trying to run this;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
 {
        DosyadanOku("c:\\sucdefteri.txt");

        Console.ReadLine();
    }

    public void DosyadanOku(string sucdefteri)
    {
        StreamReader dosyaOku = new StreamReader(@"C:\\sucdefteri.txt");

            string metin;
            metin = dosyaOku.ReadLine();

            string kesme = "\t";
            char[] b = kesme.ToCharArray();
            string[] satirlar = metin.Split(b);
            Page.ClientScript.RegisterArrayDeclaration("Skills", satirlar[2]);


        dosyaOku.Close();

}
}

at my server side of ASP.net page. This simply gets a line from sucdefteri.txt, then cuts a part of it and passes it to client side of page with

Page.ClientScript.RegisterArrayDeclaration("Skills", satirlar[2]); . I mean it should, but it can't. If I try this file reading/cutting code as a console application, it's working fine, getting the part from the line and print it to console. But it's not working with this. And also if I add a code like this;

satirlar[2] ="'izmit'";

and then send it to client, it's working. So I think something is wrong with the file reading.

Thank you for your helps.

use Server.MapPath(" ")
like this

StreamWriter _testData = new StreamWriter(Server.MapPath("~/data.txt"), true);
    _testData.WriteLine(TextBox1.Text); // Write the file.
    _testData.Flush();
    _testData.Close(); // Close the instance of StreamWriter.
    _testData.Dispose(); // Dispose from memory.       

The question doesn't seem clear, but I believe you want to read/write a text file in asp.net. If yes, try this:

    public void WriteTextFile(String FileName)
    {
    TextWriter objTextWriter= new StreamWriter(FileName);
    objTextWriter.WriteLine(DateTime.Now); //Writing current time in textfile
    objTextWriter.Close();
    }
    public String ReadTextFile(String FileName)
    {
    Textreader objTextReader= new StreamReader(FileName);
    String fileContent = objTextWriter.ReadLine();
    objTextReader.Close();
    return fileContent;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM