Home Python C Language C ++ HTML 5 CSS Javascript Java Kotlin SQL DJango Bootstrap React.js R C# PHP ASP.Net Numpy Dart Pandas Digital Marketing

ASP.NET Reference Files:


In ASP.NET, you can work with individual files on the server's file system using the System.IO.File class. This class provides methods for tasks such as creating, deleting, moving, copying, reading, and writing files.

Here's an example demonstrating how to work with files in ASP.NET:

Example

Imports System.IO

Public Class FileExample
Public Shared Sub WriteToFile(ByVal filePath As String, ByVal content As String)
' Write content to a file
File.WriteAllText(filePath, content)
End Sub

Public Shared Function ReadFromFile(ByVal filePath As String) As String
' Read content from a file
Return File.ReadAllText(filePath)
End Function

Public Shared Sub CopyFile(ByVal sourceFilePath As String, ByVal destinationFilePath As String)
' Copy a file from source to destination
File.Copy(sourceFilePath, destinationFilePath)
End Sub

Public Shared Sub DeleteFile(ByVal filePath As String)
' Delete a file
File.Delete(filePath)
End Sub
End Class

Explanation of the code:

Here's how you can use these methods in your ASP.NET application:

Example

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
' Define the file paths
Dim sourceFilePath As String = Server.MapPath("~/App_Data/source.txt")
Dim destinationFilePath As String = Server.MapPath("~/App_Data/destination.txt")

' Write content to the file
FileExample.WriteToFile(sourceFilePath, "Hello, world!")

' Read content from the file
Dim content As String = FileExample.ReadFromFile(sourceFilePath)

' Display the content on the page
Response.Write("Content read from the file: " & content & "<br/>")

' Copy the file
' FileExample.CopyFile(sourceFilePath, destinationFilePath)

' Delete the file
' FileExample.DeleteFile(sourceFilePath)
End Sub

In this example:


Advertisement
Advertisement





Q3 Schools : India


Online Complier

HTML 5

Python

java

C++

C

JavaScript

Website Development

HTML

CSS

JavaScript

Python

SQL

Campus Learning

C

C#

java