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?
Loading
VulpesPosted Sep 1, 2012, 1:54 PM
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
scampercatPosted Sep 1, 2012, 1:39 PM
Can you tell me why I would use the StreamWriter?
VulpesPosted Sep 1, 2012, 1:12 PM
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