3
Answers

C# 2008 dynamic path

scampercat

scampercat

12y
2.1k
1
Since I am new to working with C# 2008 desktop application, I have the following questions to ask since I need place files in a directory path that looks like D://app1/test/mm-dd-yyyy/customer number/type of document being worked on:

1. How would you code the month-date-year format (mm-dd-yyyy) part of the path?
2. The path of the path that stays the same is D://app1/test/. The dynamic part of the path is mm-dd-yyyy/customer number/type of document being worked on.

Thus can you tell me how to code the static part of the path and setup the dynamic part of the path so that I can place files in the dymanic part of the path?

3. Once I have the new path location, can you tell me how to place files in the new directory path location?
Answers (3)
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
First of all, it's just dawned on me that you'll need to create the directory before you can save any files to it. So, I'd change the above method to get the full directory path:

string GetDirectoryPath(string customerNumber, string typeOfDocument)
{
    string staticPath = "D:/app1/test";
    string date = DateTime.Today.ToString("MM-dd-yyyy");
    return String.Format("{0}/{1}/{2}/{3}/", staticPath, date, customerNumber, typeOfDocument);

You can then use System.IO.Directory.CreateDirectory(dirPath) to create it.

If you want to create a file (say scampercat.txt) in that directory, the file path will of course be:

   string filePath = dirPath + "/" + "scampercat.txt";

Suppose you have an array containing three lines that you'd like to write to the file:

   string[] array = {"First line", "Second line", "Third line"};

You can then create the file and write those lines to it with:

   System.IO.File.WriteAllLines(filePath, array);

Alternatively, you can do the same thing with a StreamWriter:

   StreamWriter sw = new StreamWriter(filePath);
   foreach(string line in array) sw.WriteLine(line);
   sw.Close();

If you have an existing file that you'd like to move to the new directory, then you can use the System.IO.File.Move method:

http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx

Accepted
0
scampercat

scampercat

NA 189 0 12y
Can you tell me what you mean by, "To save a file to that location, if you have an array containing all the lines of the file"?

Can you tell me why I would use the StreamWriter?
0
Vulpes

Vulpes

NA 98.3k 1.5m 12y
The following method should create a full path for a given file:

string GetFilePath(string customerNumber, string typeOfDocument, string fileName)
{
    string staticPath = "D:/app1/test";
    string date = DateTime.Today.ToString("MM-dd-yyyy");
    return String.Format("{0}/{1}/{2}/{3}/{4}", staticPath, date, customerNumber, typeOfDocument, fileName);


To save a file to that location, if you have an array containing all the lines of the file, then you can do:

   System.IO.File.WriteAllLines(filePath, array);

or, if you have a string, containing all the text of the file:

   System.IO.File.WriteAllText(filePath, text);

If you have neither, then you can use a StreamWriter to create and write to the file:

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx