{"id":173,"date":"2012-02-02T11:21:55","date_gmt":"2012-02-02T03:21:55","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=173"},"modified":"2018-09-03T21:27:52","modified_gmt":"2018-09-03T13:27:52","slug":"how-to-create-a-file-in-c","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-create-a-file-in-c\/","title":{"rendered":"How to create a file in C#"},"content":{"rendered":"<p>There are a few ways to create a file in C#. This post documents 3 ways to do that:<\/p>\n<ol>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.fileinfo.aspx\" title=\"Link to msdn reference for the System.IO.FileInfo class\" target=\"_blank\"><code>System.IO.FileInfo<\/code><\/a> class<\/li>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.file.aspx\" title=\"Link to msdn reference for the System.IO.File class\" target=\"_blank\"><code>System.IO.File<\/code><\/a> class<\/li>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.filestream.aspx\" title=\"Link to msdn reference for the System.IO.FileStream class\" target=\"_blank\"><code>System.IO.FileStream<\/code><\/a> class<\/li>\n<\/ol>\n<h3>Create a file using System.IO.FileInfo<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.IO;\r\n\r\npublic class CreateFileWithFileInfo\r\n{\r\n    public static void Main() \r\n    {\r\n        \/\/ Creates a file named techcoil.txt in the same directory as\r\n        \/\/ the executing binary.\r\n        FileInfo fileInfo = new FileInfo(&amp;quot;techcoil.txt&amp;quot;);\r\n        FileStream fStream = fileInfo.Create();\r\n        fStream.Close();\r\n    }\r\n}\r\n<\/pre>\n<h4>Behaviour of code snippet<\/h4>\n<ul>\n<li>If file does not exist, a new one will be created.<\/li>\n<li>If file exists, it will be overwritten.<\/li>\n<li>If file exists and is currently opened by another process, a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.ioexception.aspx\" title=\"Link to System.IO.IOexception\" target=\"_blank\">System.IO.IOException<\/a> will be thown.<\/li>\n<\/ul>\n<h3>Create a file using System.IO.File class<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nusing System;\r\nusing System.IO;\r\n\r\npublic class CreateFileWithFile\r\n{\r\n    public static void Main() \r\n    {\r\n        \/\/ Creates a file named techcoil.txt in the same directory as\r\n        \/\/ the executing binary.\r\n        FileStream fStream = File.Create(&amp;quot;techcoil.txt&amp;quot;);\r\n        fStream.Close();\r\n    }\r\n}\r\n<\/pre>\n<h4>Behaviour of code snippet<\/h4>\n<ul>\n<li>If file does not exist, a new one will be created.<\/li>\n<li>If file exists, it will be overwritten.<\/li>\n<li>If file exists and is currently opened by another process, a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.ioexception.aspx\" title=\"Link to System.IO.IOexception\" target=\"_blank\">System.IO.IOException<\/a> will be thown.<\/li>\n<\/ul>\n<h3>Create a file using System.IO.FileStream class<\/h3>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic class CreateFileWithFileStream\r\n{\r\n    public static void Main() \r\n    {\r\n        \/\/ Creates a file named techcoil.txt in the same directory as\r\n        \/\/ the executing binary.\r\n        FileStream fStream = File.Create(&amp;quot;techcoil.txt&amp;quot;);\r\n        fStream.Close();\r\n    }\r\n}\r\n<\/pre>\n<h4>Behaviour of code snippet<\/h4>\n<ul>\n<li>If file does not exist, a new one will be created.<\/li>\n<li>If file exists, it will be overwritten.<\/li>\n<li>If file exists and is currently opened by another process, a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.ioexception.aspx\" title=\"Link to System.IO.IOexception\" target=\"_blank\">System.IO.IOException<\/a> will be thown.<\/li>\n<\/ul>\n<h3>Related posts<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-write-to-file-in-c\/\" title=\"How to write to file in C#\">How to write to file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/how-to-read-from-file-in-c\/\" title=\"How to read from file in C#\">How to read from file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/quick-references\/c-facilities-for-dealing-with-folders\/\" title=\"C# facilities for dealing with folders\" target=\"_blank\">C# facilities for dealing with folders<\/a>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-2N\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-2N&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-2N&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F173&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>There are a few ways to create a file in C#. This post documents 3 ways to do that:<\/p>\n<ol>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.fileinfo.aspx\" title=\"Link to msdn reference for the System.IO.FileInfo class\" target=\"_blank\">System.IO.FileInfo<\/a> class<\/li>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.file.aspx\" title=\"Link to msdn reference for the System.IO.File class\" target=\"_blank\">System.IO.File<\/a> class<\/li>\n<li>Via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.filestream.aspx\" title=\"Link to msdn reference for the System.IO.FileStream class\" target=\"_blank\">System.IO.FileStream<\/a> class<\/li>\n<\/ol>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,88,95,85,86,87],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-2N","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/173"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=173"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/173\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=173"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=173"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=173"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}