{"id":202,"date":"2010-04-28T19:04:11","date_gmt":"2010-04-28T19:04:11","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=202"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"working-with-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/file-input-output-serialization\/working-with-files","title":{"rendered":"Working With Files"},"content":{"rendered":"<pre><code>\r\nusing namespace System::IO;\r\n<\/code><\/pre>\n<h4>Does file exist?<\/h4>\n<pre><code>\r\n\tif (File::Exists(\"c:\\\\temp.txt\"))\r\n<\/code><\/pre>\n<h4>Copy File<\/h4>\n<pre><code>\r\n\tFile::Copy(\"c:\\\\temp1.txt\", \"c:\\\\temp2.txt\");\r\n<\/code><\/pre>\n<h4>Move File<\/h4>\n<pre><code>\r\n\tFile::Move(\"c:\\\\dir1\\\\temp1.txt\", \"c:\\\\dir2\\\\temp1.txt\");\t\/\/Source, Destination\r\n<\/code><\/pre>\n<h4>Delete File<\/h4>\n<pre><code>\r\n\ttry\r\n\t{\r\n\t\tif (File::Exists(\"c:\\\\temp.txt\"))\r\n\t\t{\r\n\t\t\t\/\/If file has read only attribute set clear it so that delete can occur\r\n\t\t\tif ((File::GetAttributes(\"c:\\\\temp1.txt\") &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly)\r\n\t\t\t\tFile::SetAttributes(\"c:\\\\temp1.txt\", FileAttributes::Normal);\r\n\r\n\t\t\tFile::Delete(\"c:\\\\temp1.txt\");\r\n\t\t}\r\n\t}\r\n\tcatch (IOException ^)\r\n\t{\r\n\t\tMessageBox::Show(L\"This file is in use by another application - please close it first\", L\"Can't overwrite file\", MessageBoxButtons::OK, MessageBoxIcon::Exclamation);\r\n\t}\r\n<\/code><\/pre>\n<h4>Rename File<\/h4>\n<pre><code>\r\n\tFile::Move(\r\n\t\t\t\"c:\\\\temp1.txt\",\t\t\t\/\/Source\r\n\t\t\t\"c:\\\\temp1.txt\"\t\/\/Destination\r\n\t\t\t);\r\n<\/code><\/pre>\n<h4>Get File Size<\/h4>\n<pre><code>\r\n\tSystem::IO::FileInfo ^FileInfo1 = gcnew System::IO::FileInfo(FilenameWithPath);\r\n\tFileSize = FileInfo1-&gt;Length;\r\n<\/code><\/pre>\n<h4>Get File Attributes<\/h4>\n<pre><code>\r\nif ((File::GetAttributes(FilenameWithPath) &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly)\r\n\t\t\t\t\t\tReadOnly = true;\r\n<\/code><\/pre>\n<h4>Open \/ Close File Dialog Box<\/h4>\n<p>See Dialog Boxes&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>using namespace System::IO; Does file exist? if (File::Exists(&#8220;c:\\\\temp.txt&#8221;)) Copy File File::Copy(&#8220;c:\\\\temp1.txt&#8221;, &#8220;c:\\\\temp2.txt&#8221;); Move File File::Move(&#8220;c:\\\\dir1\\\\temp1.txt&#8221;, &#8220;c:\\\\dir2\\\\temp1.txt&#8221;); \/\/Source, Destination Delete File try { if (File::Exists(&#8220;c:\\\\temp.txt&#8221;)) { \/\/If file has read only attribute set clear it so that delete can occur if ((File::GetAttributes(&#8220;c:\\\\temp1.txt&#8221;) &amp; FileAttributes::ReadOnly) == FileAttributes::ReadOnly) File::SetAttributes(&#8220;c:\\\\temp1.txt&#8221;, FileAttributes::Normal); File::Delete(&#8220;c:\\\\temp1.txt&#8221;); } } catch (IOException ^) { MessageBox::Show(L&#8221;This [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[],"class_list":["post-202","post","type-post","status-publish","format-standard","hentry","category-file-input-output-serialization"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=202"}],"version-history":[{"count":10,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/202\/revisions"}],"predecessor-version":[{"id":1126,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/202\/revisions\/1126"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}