| .NET File Manipulation Posted by: Xav in Untagged on Jul 02, 2008 |
Being able to interact with the user's file system is very useful. Most programs incooperate some sort of load/save facility, and handling files is extremely important. Luckily, .NET has made it a lot easier for us. Here's some of the things it can do:
Manipulating Files
It is handy to be able to copy, move or rename files. The .NET Framework has a namespace called System.IO. "IO" is short for Input/Output, in case you're interested. Within this namespace, there is a static class called File. This contains plenty of static methods for interacting with files.
Accessing the functions is used by adding the following code to the top of the code file (this is in C#):

Now, all the classes are imported, and we can use the File object whereever we like in the code file to access the methods.
To copy a file (perhaps for a backup application), use the Copy() function:

As you can see, Copy() takes two parameters - the source file name and the destination filename. The reason we use the @ symbol is because the filenames contain symbols (backslashes). These are interpreted as escape sequences, so to produce the correct results we preceed the string with @ to show we want the actual backslash character, not an escape sequence.
To move a file, you simply use the Move() method. Again, this takes two parameters:

To rename a file, simply use the Move() method, but pass in a different filename, as demonstrated above.
To delete a file, use the Delete() function. This accepts just one parameter, rather than two - the file to be deleted:

Use this one with care, as files are not sent to the recycle bin - they are permanently deleted. If you're testing out this method, create a sample file to experiment deleting on.
And that's it. File manipulation just got a whole lot simpler, .NET style.
Manipulating Files
It is handy to be able to copy, move or rename files. The .NET Framework has a namespace called System.IO. "IO" is short for Input/Output, in case you're interested. Within this namespace, there is a static class called File. This contains plenty of static methods for interacting with files.
Accessing the functions is used by adding the following code to the top of the code file (this is in C#):

- using System.IO;
using System.IO;
Now, all the classes are imported, and we can use the File object whereever we like in the code file to access the methods.
To copy a file (perhaps for a backup application), use the Copy() function:

- File.Copy(@"C:sourcedata.xml",@"C:destinationdata.xml");
File.Copy(@"C:sourcedata.xml",@"C:destinationdata.xml");
As you can see, Copy() takes two parameters - the source file name and the destination filename. The reason we use the @ symbol is because the filenames contain symbols (backslashes). These are interpreted as escape sequences, so to produce the correct results we preceed the string with @ to show we want the actual backslash character, not an escape sequence.
To move a file, you simply use the Move() method. Again, this takes two parameters:

- File.Move(@"C:sourcemyfile.dat",@"D:destinationmycopiedfile.dat");
File.Move(@"C:sourcemyfile.dat",@"D:destinationmycopiedfile.dat");
To rename a file, simply use the Move() method, but pass in a different filename, as demonstrated above.
To delete a file, use the Delete() function. This accepts just one parameter, rather than two - the file to be deleted:

- File.Delete(@"C:oldfile.wmv");
File.Delete(@"C:oldfile.wmv");
Use this one with care, as files are not sent to the recycle bin - they are permanently deleted. If you're testing out this method, create a sample file to experiment deleting on.
And that's it. File manipulation just got a whole lot simpler, .NET style.