0

I have this html file. I want to create a text file through c# and save this value on it.

index.html

<!DOCTYPE html>
  <html>
    <head>
      <meta charset="utf-8" />
         <title></title>
    </head>
    <body>
     <form onsubmit=""  action="">
      <label>Username</label>
      <input id="username" type="text" />
      <input id="submitBtn" type="submit" value="Confirm" />
     </form>
    </body>
 </html>
2
  • Could you please confirm if you have the html as a file or you have it as a string and want to save it as a file? Commented May 10, 2017 at 5:52
  • I have it as a file Commented May 10, 2017 at 5:57

3 Answers 3

1

If your requirement is to save html string to a file, you can use:

File.WriteAllText(@"C:\words.txt", htmlString);

If you already have an HTML file and want to save it as a new file with .txt extension, use un-lucky's answer.

Sign up to request clarification or add additional context in comments.

3 Comments

But how I have to link between c# file and html file?
what do you mean by "link between"?
like in ASP.Net we use CodeBehind="WebForm1.aspx.cs" , to pass the values
0

A lot of answers all ready here , but you confusing with word "linking".
In c# you can access any asp.net control in its class file , and also html control by adding runat=server into html control but it have to be in aspx page.
Either switch to aspx to accomplish it in c# or in case of .html file you have to use javascript

You can insert below code in your html file and trigger it through a button

To get HTML of a page into variable

var v=$("body").html()

And than function to save it into a text file

function writeToFile(v)
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("data.txt", 8, false, 0);
fh.WriteLine(v);
fh.Close();
}

Pass var v into writeToFile()

2 Comments

Thanks, but where I put this call writeToFile(v) ?
There must be an html button to export html page in text file , on click of that button you can call this function .
0

You can make it through File.Copy() method, Where you have to pass the path of the .html file as source path, the path of the .txt as the destination path. then the copy method will create the required text file in the specified path. consider the following code:

 File.Copy("index.html","path\\index.txt");

3 Comments

@S.Petrosov: I have checked with the example in the question, is working fine for me. Nothing was removed. If you have option to check means, could you please check and let me know
But how I have to link between c# file and html file?
@un-lucky oh sorry it seems that when I have copied example "<","/>", and quotes were not copied for some reason, I have found it out just now when checked the html file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.