Creating Text File in Library in C#

This article will discuss applications of XML and C#. This article shows how to more easily create a text file using a library in C# using both XML and C#.

Here we go.

Functioning

This code creates a button click event for creating the text file using a library. A direct button from the toolkit is used and the text file is created on its click event.

For example:

button1_Click(object sender, RoutedEventArgs e)

Then in the event there will be a text file like in my case it is abhi.txt, that will directly get called on the click event.

click event

Procedure

First of all use the following procedure:

  • Open Visual Studio
  • Create a .CS file; for example:

    CS file

  • Name it as you choose, such as abc.cs
  • Now the code in this .CS page is like this
  • In that .cs file page create a button
  • The code for the click event:

Coding on click event 

// end of procedure

Code
 

public MainPage()

{

    this.InitializeComponent();

    // initializing component

}

/// <summary>

/// Invoked when this page is about to be displayed in a Frame.

/// </summary>

/// <param name="e">Event data that describes how this page was reached. The Parameter

/// property is typically used to configure the page.</param>

 

protected override void OnNavigatedTo(NavigationEventArgs e)

{

}

private async void button1_Click(object sender, RoutedEventArgs e)

//functioning of click event

{

    StorageFolder storefolder = KnownFolders.DocumentsLibrary;

    StorageFile testfile = await storefolder.CreateFileAsync("abhi.txt",CreationCollisionOption.Replac Existing);

    // functioning & calling

    await FileIO.WriteTextAsync(testfile, "I love C# corner");

}

Up Next
    Ebook Download
    View all
    Learn
    View all