Skip to content
- Tutorials
- Python Tutorial
- Taking Input in Python
- Python Operators
- Python Data Types
- Python Loops and Control Flow
- Python Functions
- Python OOPS Concept
- Python Data Structures
- Python Exception Handling
- Python File Handling
- Python Exercises
- Java
- Java Programming Language
- Java Collections
- Java 8 Tutorial
- Java Programs
- Java Interview Questions
- Java Exercises
- Java Quiz
- Java Projects
- Advance Java
- Programming Languages
- System Design
- Interview Corner
- Computer Science Subjects
- DevOps
- Linux
- Software Testing
- Databases
- Android
- Excel
- Mathematics
- Python Tutorial
- DSA
- Data Structures
- Algorithms
- Analysis of Algorithms
- Searching Algorithms
- Sorting Algorithms
- Greedy Algorithms
- Dynamic Programming
- Graph Algorithms
- Pattern Searching
- Recursion
- Backtracking
- Divide and Conquer
- Mathematical Algorithms
- Geometric Algorithms
- Bitwise Algorithms
- Randomized Algorithms
- Branch and Bound
- Algorithms Tutorial
- DSA Tutorial
- Practice
- All DSA Problems
- Problem of the Day
- Company Wise Coding Practice
- GfG SDE Sheet
- Practice Problems Difficulty Wise
- Language Wise Coding Practice
- Curated DSA Lists
- Competitive Programming
- Company Wise SDE Sheets
- DSA Cheat Sheets
- Top Interview Questions
- Puzzles
- Data Science
- Web Tech
- Courses
-
Last Updated : 11 Oct, 2021
Comments
Improve
File.Exists(String) is an inbuilt File class method that is used to determine whether the specified file exists or not. This method returns true if the caller has the required permissions and path contains the name of an existing file; otherwise, false. Also, if the path is null, then this method returns false.
Syntax:
public static bool Exists (string path);
Here, path is the specified path that is to be checked.
Program 1: Before running the below code, a file file.txt is created with some contents shown below:
CSharp
// C# program to illustrate the usage
// of File.Exists(String) method
// Using System and System.IO namespaces
using
System;
using
System.IO;
class
GFG {
static
void
Main()
{
// Checking the existence of the specified
if
(File.Exists(
"file.txt"
)) {
Console.WriteLine(
"Specified file exists."
);
}
See AlsoHow to Get Value from HashTable Collection in C# using Specified KeyHow to check if a hash contains a particular key in RubyC# | Strings | .Contains() | CodecademyC# | Check if a SortedList object contains a specific value - GeeksforGeekselse
{
Console.WriteLine(
"Specified file does not "
+
"exist in the current directory."
);
}
}
}
Output:
Specified file exists.
Program 2: Before running the below code, no file is created.
CSharp
// C# program to illustrate the usage
// of File.Exists(String) method
// Using System and System.IO namespaces
using
System;
using
System.IO;
class
GFG {
static
void
Main()
{
// Checking the existence of the specified
if
(File.Exists(
"file.txt"
)) {
Console.WriteLine(
"Specified file exists."
);
}
else
{
Console.WriteLine(
"Specified file does not"
+
" exist in the current directory."
);
}
}
}
Output:
Specified file does not exist in the current directory.
Please Login to comment...
Similar Reads
File.GetLastWriteTimeUtc() Method in C# with Examples
File.GetLastWriteTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
2 min read
File.Create(String, Int32, FileOptions, FileSecurity) Method in C# with Examples
File.Create(String, Int32, FileOptions, FileSecurity) is an inbuilt File class method which is used to overwrite an existing file, specifying a buffer size and options that describe how to create or overwrite the file and value that determines the access control and audit security for the file. If the specified file is not existing, this function i
3 min read
File.Open(String, FileMode, FileAccess) Method in C# with Examples
File.Open(String, FileMode, FileAccess) is an inbuilt File class method that is used to open a FileStream on the specified path with the specified mode and access with no sharing.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess access); Parameter: This function accepts three parameters whic
3 min read
File.Open(String, FileMode, FileAccess, FileShare) Method in C# with Examples
File.Open(String, FileMode, FileAccess, FileShare) is an inbuilt File class method that is used to open a FileStream on the specified path, having the specified mode with read, write, or read/write access and the specified sharing option.Syntax: public static System.IO.FileStream Open (string path, System.IO.FileMode mode, System.IO.FileAccess acce
3 min read
File.Copy(String, String) Method in C# with Examples
File.Copy(String, String) is an inbuilt File class method that is used to copy the content of the existing source file content to another destination file which is created by this function. Syntax: public static void Copy (string sourceFileName, string destFileName); Parameter: This function accepts two parameters which are illustrated below: sourc
3 min read
File.Move() Method in C# with Examples
File.Move() is an inbuilt File class method that is used to move a specified file to a new location. This method also provides the option to specify a new file name. Syntax: public static void Move (string sourceFileName, string destFileName); Parameter: This function accepts two parameters which are illustrated below: sourceFileName: This is the s
2 min read
File.AppendText() Method in C# with Examples
File.AppendText() is an inbuilt File class method which is used to create a StreamWriter that appends UTF-8 encoded text to an existing file else it creates a new file if the specified file does not exist.Syntax: public static System.IO.StreamWriter AppendText (string path); Parameter: This function accepts a parameter which is illustrated below: p
3 min read
File.CreateText() Method in C# with Examples
File.CreateText() is an inbuilt File class method that is used to overwrite the contents of the existing file with the given UTF-8 encoded text and if the file is not created already, this function will create a new file with the specified contents. Syntax: public static System.IO.StreamWriter CreateText (string path); Parameter: This function acce
3 min read
File.Delete() Method in C# with Examples
File.Delete(String) is an inbuilt File class method which is used to delete the specified file.Syntax: public static void Delete (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file path which is to be deleted. Exceptions: ArgumentException: The path is a zero-length string, contai
3 min read
File.OpenRead() Method in C# with Examples
File.OpenRead(String) is an inbuilt File class method which is used to open an existing file for reading.Syntax: public static System.IO.FileStream OpenRead (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file which is going to be opened for reading. Exceptions: ArgumentException:
3 min read
File.OpenWrite() Method in C# with Examples
File.OpenWrite(String) is an inbuilt File class method that is used to open an existing file or creates a new file for writing. Syntax: public static System.IO.FileStream OpenWrite (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified text file that is going to be opened for writing. Exc
3 min read
File.OpenText() Method in C# with Examples
File.OpenText(String) is an inbuilt File class method which is used to open an existing UTF-8 encoded text file for reading.Syntax: public static System.IO.StreamReader OpenText (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified text file which is going to be opened for reading. Excep
2 min read
File.GetLastAccessTime() Method in C# with Examples
File.GetLastAccessTime(String) is an inbuilt File class method which is used to return the date and time the specified file or directory was last accessed.Syntax: public static DateTime GetLastAccessTime (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file path. Exceptions: Unautho
2 min read
File.GetLastAccessTimeUtc() Method in C# with Examples
File.GetLastAccessTimeUtc(String) is an inbuilt File class method which is used to return the date and time, in coordinated universal time (UTC), that the specified file or directory was last accessed.Syntax: public static DateTime GetLastAccessTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: Thi
2 min read
File.AppendAllText(String, String) Method in C# with Examples
File.AppendAllText(String, String) is an inbuilt File class method which is used to append the specified string to the given file if that file exists else creates a new file and then appending is done. It also closes the file.Syntax: public static void AppendAllText (string path, string contents); Parameter: This function accepts two parameters whi
3 min read
File.AppendAllText(String, String, Encoding) Method in C# with Examples
File.AppendAllText(String, String, Encoding) is an inbuilt File class method which is used to append the specified string to the given file using the specified encoding if that file exists else creates a new file and then appending is done. Syntax: public static void AppendAllText (string path, string contents, System.Text.Encoding encoding); Param
3 min read
File.GetCreationTime() Method in C# with Examples
File.GetCreationTime(String) is an inbuilt File class method which is used to return the creation date and time of the specified file or directory. Syntax: public static DateTime GetCreationTime (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file path whose creation date and time
2 min read
File.GetCreationTimeUtc() Method in C# with Examples
File.GetCreationTimeUtc(String) is an inbuilt File class method which is used to return the creation date and time, in coordinated universal time (UTC), of the specified file or directory. Syntax: public static DateTime GetCreationTimeUtc (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the speci
2 min read
File.GetLastWriteTime() Method in C# with Examples
File.GetLastWriteTime(String) is an inbuilt File class method which is used to return the date and time the specified file or directory was last written to.Syntax: public static DateTime GetLastWriteTime (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file path. Exceptions: Unautho
2 min read
File.ReadAllBytes() Method in C# with Examples
File.ReadAllBytes(String) is an inbuilt File class method that is used to open a specified or created binary file and then reads the contents of the file into a byte array and then closes the file.Syntax: public static byte[] ReadAllBytes (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the speci
2 min read
File.SetLastAccessTime() Method in C# with Examples
File.SetLastAccessTime(String, DateTime) is an inbuilt File class method that is used to set the date and time the specified file was last accessed. Syntax: public static void SetLastAccessTime (string path, DateTime lastAccessTime); Parameter: This function accepts two parameters which are illustrated below: path: This is the file for which to set
3 min read
File.SetLastAccessTimeUtc() Method in C# with Examples
File.SetLastAccessTimeUtc(String, DateTime) is an inbuilt File class method which is used to set the date and time, in coordinated universal time (UTC), that the specified file was last accessed.Syntax: public static void SetLastAccessTimeUtc (string path, DateTime lastAccessTimeUtc); Parameter: This function accepts two parameters which are illust
3 min read
File.SetCreationTime() Method in C# with Examples
File.SetCreationTime(String, DateTime) is an inbuilt File class method that is used to set the local date and time the file was created.Syntax: public static void SetCreationTime (string path, DateTime creationTime); Parameter: This function accepts two parameters which are illustrated below: path: The specified file for which to set the creation d
3 min read
File.SetCreationTimeUtc() Method in C# with Examples
File.SetCreationTimeUtc(String, DateTime) is an inbuilt File class method that is used to set the date and time, in coordinated universal time (UTC), that the file was created.Syntax: public static void SetCreationTimeUtc (string path, DateTime creationTimeUtc); Parameter: This function accepts two parameters which are illustrated below: path: The
3 min read
File.SetLastWriteTime() Method in C# with Examples
File.SetLastWriteTime(String) is an inbuilt File class method which is used to set the date and time that the specified file was last written to. Syntax: public static void SetLastWriteTime (string path, DateTime lastWriteTime); Parameter: This function accepts two parameters which are illustrated below: path: This is the specified file for which t
3 min read
File.GetAttributes() Method in C# with Examples
File.GetAttributes(String) is an inbuilt File class method that is used to get the file attributes of the file on the path. File attributes are those certain rights that are either granted or denied. These rights are for a user or for an operating system that accesses the file. These attributes are such as Read-only, Archive, System, Hidden etc. Sy
3 min read
File.SetLastWriteTimeUtc() Method in C# with Examples
File.SetLastWriteTimeUtc(String) is an inbuilt File class method which is used to set the date and time, in coordinated universal time (UTC), that the specified file was last written to.Syntax: public static void SetLastWriteTimeUtc (string path, DateTime lastWriteTime); Parameter: This function accepts two parameters which are illustrated below: p
3 min read
File.SetAttributes() Method in C# with Examples
File.SetAttributes(String, FileAttributes) is an inbuilt File class method that is used to set the specified file attributes of the file on the specified path. File attributes are those certain rights that are either granted or denied. These rights are for a user or for an operating system that accesses the file. These attributes are such as Read-o
3 min read
File.WriteAllBytes() Method in C# with Examples
File.WriteAllBytes(String) is an inbuilt File class method that is used to create a new file then writes the specified byte array to the file and then closes the file. If the target file already exists, it is overwritten. Syntax: public static void WriteAllBytes (string path, byte[] bytes); Parameter: This function accepts two parameters which are
3 min read
File.ReadAllLines(String) Method in C# with Examples
File.ReadAllLines(String) is an inbuilt File class method that is used to open a text file then reads all lines of the file into a string array and then closes the file.Syntax: public static string[] ReadAllLines (string path); Parameter: This function accepts a parameter which is illustrated below: path: This is the specified file to open for read
2 min read
Article Tags :
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, check: true }), success:function(result) { jQuery.ajax({ url: writeApiUrl + 'suggestions/auth/' + `${post_id}/`, type: "GET", dataType: 'json', xhrFields: { withCredentials: true }, success: function (result) { $('.spinner-loading-overlay:eq(0)').remove(); var commentArray = result; if(commentArray === null || commentArray.length === 0) { // when no reason is availaible then user will redirected directly make the improvment. // call to api create-improvement-post $('body').append('
'); $('.spinner-loading-overlay').show(); jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id, }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.unlocked-status--improve-modal-content').css("display","none"); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); return; } var improvement_reason_html = ""; for(var comment of commentArray) { // loop creating improvement reason list markup var comment_id = comment['id']; var comment_text = comment['suggestion']; improvement_reason_html += `
${comment_text}
`; } $('.improvement-reasons_wrapper').html(improvement_reason_html); $('.improvement-bottom-btn').html("Create Improvement"); $('.improve-modal--improvement').hide(); $('.improvement-reason-modal').show(); }, error: function(e){ $('.spinner-loading-overlay:eq(0)').remove(); // stop loader when ajax failed; }, }); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); } else { if(loginData && !loginData.isLoggedIn) { $('.improve-modal--overlay').hide(); if ($('.header-main__wrapper').find('.header-main__signup.login-modal-btn').length) { $('.header-main__wrapper').find('.header-main__signup.login-modal-btn').click(); } return; } } }); $('.left-arrow-icon_wrapper').on('click',function(){ if($('.improve-modal--suggestion').is(":visible")) $('.improve-modal--suggestion').hide(); else{ $('.improvement-reason-modal').hide(); } $('.improve-modal--improvement').show(); }); function loadScript(src, callback) { var script = document.createElement('script'); script.src = src; script.onload = callback; document.head.appendChild(script); } function suggestionCall() { var suggest_val = $.trim($("#suggestion-section-textarea").val()); var array_String= suggest_val.split(" ") var gCaptchaToken = $("#g-recaptcha-response-suggestion-form").val(); var error_msg = false; if(suggest_val != "" && array_String.length >=4){ if(suggest_val.length <= 2000){ var payload = { "gfg_post_id" : `${post_id}`, "suggestion" : `
${suggest_val}
`, } if(!loginData || !loginData.isLoggedIn) // User is not logged in payload["g-recaptcha-token"] = gCaptchaToken jQuery.ajax({ type:'post', url: "https://apiwrite.geeksforgeeks.org/suggestions/auth/create/", xhrFields: { withCredentials: true }, crossDomain: true, contentType:'application/json', data: JSON.stringify(payload), success:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-section-textarea').val(""); jQuery('.suggest-bottom-btn').css("display","none"); // Update the modal content const modalSection = document.querySelector('.suggestion-modal-section'); modalSection.innerHTML = `
Thank You!
Your suggestions are valuable to us.
You can now also contribute to the GeeksforGeeks community by creating improvement and help your fellow geeks.
`; }, error:function(data) { jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Something went wrong."); jQuery('#suggestion-modal-alert').show(); error_msg = true; } }); } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Minimum 5 Words and Maximum Character limit is 2000."); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } } else{ jQuery('.spinner-loading-overlay:eq(0)').remove(); jQuery('#suggestion-modal-alert').html("Enter atleast four words !"); jQuery('#suggestion-modal-alert').show(); jQuery('#suggestion-section-textarea').focus(); error_msg = true; } if(error_msg){ setTimeout(() => { jQuery('#suggestion-section-textarea').focus(); jQuery('#suggestion-modal-alert').hide(); }, 3000); } } document.querySelector('.suggest-bottom-btn').addEventListener('click', function(){ jQuery('body').append('
'); jQuery('.spinner-loading-overlay').show(); if(loginData && loginData.isLoggedIn) { suggestionCall(); return; } // load the captcha script and set the token loadScript('https://www.google.com/recaptcha/api.js?render=6LdMFNUZAAAAAIuRtzg0piOT-qXCbDF-iQiUi9KY', function() { setGoogleRecaptcha(); }); }); $('.improvement-bottom-btn.create-improvement-btn').click(function() { //create improvement button is clicked $('body').append('
'); $('.spinner-loading-overlay').show(); // send this option via create-improvement-post api jQuery.ajax({ url: writeApiUrl + 'create-improvement-post/?v=1', type: "POST", contentType: 'application/json; charset=utf-8', dataType: 'json', xhrFields: { withCredentials: true }, data: JSON.stringify({ gfg_id: post_id }), success:function(result) { $('.spinner-loading-overlay:eq(0)').remove(); $('.improve-modal--overlay').hide(); $('.improvement-reason-modal').hide(); $('.create-improvement-redirection-to-write').attr('href',writeUrl + 'improve-post/' + `${result.id}` + '/', '_blank'); $('.create-improvement-redirection-to-write')[0].click(); }, error:function(e) { $('.spinner-loading-overlay:eq(0)').remove(); var result = e.responseJSON; if(result.detail.non_field_errors.length){ $('.improve-modal--improve-content .improve-modal--improve-content-modified').text(`${result.detail.non_field_errors}.`); jQuery('.improve-modal--overlay').show(); jQuery('.improve-modal--improvement').show(); $('.locked-status--impove-modal').css("display","block"); $('.unlocked-status--improve-modal-content').css("display","none"); $('.improve-modal--improvement').attr("status","locked"); $('.improvement-reason-modal').hide(); } }, }); });
Continue without supporting 😢