FIO Helper Class
This is an optional section. It was much more important before we realized how easily we much simplified file manipulating projects by changing the Output Path setting.
Still there are a variety of situations where a program may want to access
resources in the file system, without know ahead of time exactly which folder contains
the file resource. The FIO
library class that we display here illustrates how
a program can search for the directory in which a file resides, given a list
of folder options. This section also illustrates use of methods from the Path
and
File
classes.
In this case we are specifically thinking of two possible uses of
program source files: When executing, the project folder may
be the current directory or, in the default setup for Xamarain, the current directory
may be two folders down in bin/Debug
.
We use one idea that is discussed more in the next chapter: We need a sequence
of directory strings to look through. At this point we have only discussed
sequences of individual characters. The variable paths
contains a sequence of directory paths to check.
(More on the syntax comes up shortly in Arrays.)
In our case we make the
sequence contain
"."
, the current directory, ".."
, the parent directory, and
Path.Combine("..", "..")
, the parent’s parent.
We make paths
a static variable, so
it is visible in all the functions in the class.
Then the sequence paths
can be used in the foreach
loop:
/// Find a directory containing the filename
/// and return the full file path, if it exists.
/// Otherwise return null.
public static string GetPath(string filename)
{
foreach (string dir in paths) {
string filePath = Path.Combine(dir, filename);
if (File.Exists(filePath))
return filePath;
}
return null;
}
For each directory path in paths
, we create a filePath
as if the
file were in that directory. We return the first path that actually exists.
We allow for the file to not be in any of the directories in paths
. If
we do not find it, we return null
(no object).
For convenience, we have an elaboration, using GetPath
,
that directly opens the file to read:
/// Find a directory containing filename;
/// return a new StreamReader to the file
/// or null if the file does not exist.
public static StreamReader OpenReader(string filename)
{
string filePath = GetPath(filename);
if (filePath == null)
return null;
else
return new StreamReader( filePath);
}
We have a variation on GetPath
that just return the path to the
directory containing the file. Here is the heading:
/// Return a directory conaining the filename, if it exists.
/// Otherwise return null.
public static string GetLocation(string filename)
This is useful in case you want to later write into the same directory
that you read from. You can get a location from GetLocation
and then
write to the same directory, creating a StreamWriter
.
You can use the convenience function:
/// Join the location directory and filename;
/// open and return a StreamWriter to the file.
public static StreamWriter OpenWriter(string location, string filename)
{
string filePath = Path.Combine(location, filename);
return new StreamWriter(filePath);
}
The entire FIO
class is in
fio/fio.cs
We illustrate the use of FIO
functions in example file
fio_usage/fio_usage.cs:
using System;
using System.IO;
namespace IntroCS
{
class FIOTest
{
public static void Main(string[] args)
{
string sample = "sample.txt";
string output = "output.txt";
Console.WriteLine("Directory of {0}: {1}",
sample, FIO.GetLocation(sample));
Console.WriteLine("Path to {0}: {1}",
sample, FIO.GetPath(sample));
StreamReader reader1 = FIO.OpenReader(sample);
if (reader1 != null) {
Console.Write(reader1.ReadToEnd());
Console.WriteLine("First reader test passed.");
reader1.Close();
}
StreamReader reader2 = FIO.OpenReader(FIO.GetLocation(sample),
sample);
if (reader2 != null) {
Console.WriteLine("Second reader test passed.");
reader2.Close();
}
StreamWriter writer1 = FIO.OpenWriter(FIO.GetLocation(sample),
output);
writer1.WriteLine("File in the same directory as {0}.", sample);
writer1.Close();
Console.WriteLine("Writer test passed; file written at \n {0}",
FIO.GetPath(output));
}
}
}
If you look at the fio_usage project in our examples solution, you see that
sample.txt
is a file in the project folder. The program
ends up writing to a new file in the same (project) directory. Remember that even
though the new file output.txt
appears in the project directory, it does not appear in the
Solution pad unless you add it to the project. You can see it in the file system,
and open it if you like.
This project was created with the default set up: Output path two folders down. If you change to Output path to the main project folder, it should still work. If you open a terminal/console and go to the project directory, you can compile and run this program, and it will also work.