File Handling - potatoscript/csharp GitHub Wiki

home

Copy File
List Files of Folder
List Folder of Folder
Open File
Read File Content Create file if not exist and Read the file content
Read XML file
Running .Bat file

home

Copy-File

   System.IO.File.Copy(oldPathFile, newPathFile);

home

List-File

   System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Location);
   foreach(System.IO.FileInfo f in dir.GetFiles("*.*"))
   {
      ListViewItem item = listView1.Items.Add(f.Name);
      item.subItems.Add(Convert.ToString(f.Length));
      item.subItems.Add(f.Extension);
   }

home

List-Folder

   TreeNode main = treeview1.Nodes.Add("Folders in: " + Location );
   Main.Tag = "";
   foreach(System.IO.DirectoryInfo g in dir.GetDirectories())
   {
      TreeNode MainNext = Main.Nodes.Add(g.FullName);
      MainNext.Tag = (g.FullName);
   }

home

Open-File

   System.Diagnostics.Process.Start(@"c:\myFile.text");

home

Read-File

   namespace FileHandlingArticleApp
   {
      class Program
      {
          static void Main(String[] args)
          {
               if(File.Exists("myFile.txt"))
               {
                   // Write to File
                   string content = File.ReadAllText("myFile.txt");
                   Console.WriteLine("Current content of file");
                   Console.WriteLine(content);
               
                   Console.WriteLine("Please ener new content for the file");
                   string newContent = Console.ReadLine();
                   File.WriteAllText("myFile.txt", newContent);

                  // Read from File
                  using(StreamWriter sw = new StreamWriter("myFile.txt"))
                  {
                     string newContent = Console.ReadLine();
                     While(newContent != "end")
                     {
                         sw.Write(newContent + Environment.NewLine);
                         newContent = Console.ReadLine();
                     }
                  }
              } // end of file exist checking
          }
      }   
   }

home

Read-XML-File

  • create myFile.xml
<?xml version = "1.0" ?>
<data>
   <myObj> ABC </myObj>
   <machine>
      <name value="xxx">yyy</name>
   </machine>
</data>
  • Load xml file
   XmlDocument xDoc = new XmlDocument();
   xDoc.Load("c:\\myFolder\\myFile.xml");
   myObj.box.SelectedIndex = myObj.box.FindString( xDoc.SelectSingleNode("data/myObj").nnerText);
  • Read XML Node Attribute
   foreach(XmlNode node in xDoc.SelectNodes("data/machine/name"))
   {
       foreach(XmlAttribute attribute in node.Attributes)
       {
          var x = new cComboBox();
          x.Text = node.InnerText;
          x.Value = attribute.Value;
          myCombox.Items.Add(x);
       }
   }

home

Run-Batch-File

   Process proc = null;
   string batDir = string.Format(@"c:\myfolder\");
   proc = new Process();
   proc.StartInfo.WorkingDirectory = batDir;
   proc.StartInfo.FileName = "restore.bat";
   proc.StartInfo.CreateNoWindow = false;
   proc.Start();
   proc.WaitForExit();
⚠️ **GitHub.com Fallback** ⚠️