I'm using a x2 Extreme HP SC where i save log files to the FTP area of the panel.
I also want to be able to copy these log files to a USB-stick connected to the panel, using a script.
I am not familiar with C# coding and have had limited success in this, but have found an example program that works well in Visual Studio, but not in the IX Developer software.
I would appreciate any help, and hope it will be useful for others too!
""
using System;
using System.IO;
namespace DirectoryCopyToUSB
{
class DirectoryCopyToUSB
{
public static void Copy()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(@"Project Files", @"Hard Disk\LogFiles", true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
}
""
Script for copying files from FTP to USB
Re: Script for copying files from FTP to USB
One reason it might not be working in iX Developer is, iX is restricted to the 3.5 Compact Framework, whereas VS has access to all frameworks.
Here is a snippet I've used in the past in iX that worked, but it looks pretty similar to your's.
Here is a snippet I've used in the past in iX that worked, but it looks pretty similar to your's.
Code: Select all
using System.IO;
using System.Text;
void Button_Click(System.Object sender, System.EventArgs e)
{
DirectoryCopy("<SourceName>", "<DestinationName>", true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
// If the source directory does not exist, throw an exception.
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory exists, delete it.
if (Directory.Exists(destDirName))
{
Directory.Delete(destDirName, true);
}
Directory.CreateDirectory(destDirName);
// Get the file contents of the directory to copy.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
try
{
// Create the path to the new copy of the file.
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
catch (Exception) { }
}
// If copySubDirs is true, copy the subdirectories.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
// Create the subdirectory.
string temppath = Path.Combine(destDirName, subdir.Name);
// Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
Best regards,
Russ
(801) 708-6690
Technical Support
Contact Us
Beijer Electronics AB
http://www.beijerelectronics.us
Russ
(801) 708-6690
Technical Support
Contact Us
Beijer Electronics AB
http://www.beijerelectronics.us