Skip to main content

Posts

Showing posts with the label file

asp.net - How to create text file and write text programmatically

Create a text file and write text programmatically The following asp.net c# example code demonstrates how we can create a text file and write text on it programmatically in the .net framework. We can get the application's physical path using HttpRequest.PhysicalApplicationPath property as Request.PhysicalApplicationPath. We also can get the file path by adding two strings, which are the physical application path and file name with extension. In this example, we created a text file name Text.txt and programmatically wrote two lines of text in this file at run time. StreamWriter Class allows us to implement a TextWriter for writing characters to a stream in a particular encoding. In this example code, we initialize a StreamWriter object. The File class’s CreateText(path) method creates or opens a file for writing UTF-8 encoded text. this method requires passing a parameter name 'path'. The path parameter value specifies the fi...

How to check whether a file exists or not in asp.net

Check whether a file exists The File class’s Exists() method allows us to determine whether the specified file exists. File.Exists(path) method is under the System.IO namespace. So we need to include the System.IO namespace on our page before using this method. The Fileclass’s Exists() method needs to pass a parameter named 'path'. The path parameter type is a String and it represents the file to check. The path value contains the full path of a file including both folder location and file name with extension. The method returns a Boolean value. If it returns 'True' then the file exists in the web server otherwise, the file does not exist. If the path parameter describes a directory then the method returns False. Because this method can only determine file existence not for use to check whether a directory exists. This method also returns False, if any error occurs while trying to determine file existence. This method should not ...

How to copy file and overwrite in asp.net c#

File copy and overwrite .NET Framework File class’s Copy(String, String, Boolean) overloaded method allow us to copy an existing file to a new file. Overwriting a file of the same name is allowed. File class’s Copy() method exists in the System.IO namespace. The Copy method requires passing three parameters named sourceFileName, destFileName, and overwrite. The sourceFileName parameter value data type is a String which represents the file to copy. The destFileName parameter value data type is also a String which represents the name of the destination file. This parameter cannot be a directory. The overwrite parameter value data type is a Boolean. this parameter value true indicates the destination file can be overwritten. If we don't want to allow destination file overwrite then we need to set this parameter value to false. The Copy() method throws an UnauthorizedAccessException exception if the caller does not have the required perm...