Solved Trying to insert a variable into a file path

November 5, 2012 at 12:52:12
Specs: XP\7
What I am trying to do is use the date from thr datetimepicker and plug it into a file path so that a folder with the date selected can be created.

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
DateTime varDate;
varDate = dateTimePicker1.Value.Date;
//string.Format("{mm-dd-yyyy}");
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{

Directory.CreateDirectory(@"\\100-nuweb-k-006.adcs.net\logs\{varDate}\textBox1.ToString-Log-Files");

I can't figure out how to make this work, I'm sure it's something simple.


See More: Trying to insert a variable into a file path

Report •


✔ Best Answer
November 6, 2012 at 07:01:08
Consider the following:
System.Windows.Forms.MessageBox.Show(
  String.Format(@"\\100-nuweb-k-006.adcs.net\logs\{0:yyyy-MM-dd}\textBox1.ToString-Log-Files", System.DateTime.Now)
);

How To Ask Questions The Smart Way



#1
November 5, 2012 at 13:03:13
You just need to do string concatenation to build the final string. Have a look at http://stackoverflow.com/questions/... and/or Google "C++ string concatenation".

Report •

#2
November 5, 2012 at 14:44:27
This looks suspiciously like C#, and not C nor C++, so I'll move it to that category.

How To Ask Questions The Smart Way


Report •

#3
November 5, 2012 at 14:53:38
ok, i tried this, but still no luck.

Directory.CreateDirectory(@"\\100-nuweb-k-006.adcs.net\logs\"+varDate+"\textBox1.ToString-Log-Files");

is there possibly something specail that has to be done with a date variable?


Report •

Related Solutions

#4
November 5, 2012 at 23:21:38
You need to convert varDate to a string first. Have a read of the documentation of the DateTime class, particularly the various To... methods.

Report •

#5
November 6, 2012 at 06:58:42
This is what I have now but I am getting an illegal characters in path error. Any ideas>?

DateTime vardate
varDate = DateTimePicker1Value.Date;
varDate.ToString("mm-dd-yyyy")

Directory.CreateDirectory(@"\\100-nuweb-k-006.adcs.net\logs\"+varDate.ToString()+"\textBox1.ToString-Log-Files");


Report •

#6
November 6, 2012 at 07:01:08
✔ Best Answer
Consider the following:
System.Windows.Forms.MessageBox.Show(
  String.Format(@"\\100-nuweb-k-006.adcs.net\logs\{0:yyyy-MM-dd}\textBox1.ToString-Log-Files", System.DateTime.Now)
);

How To Ask Questions The Smart Way


Report •

#7
November 6, 2012 at 07:39:12
Razor, Thanks for the reply. It is almost doing what I want it to do with some modifications to your code. Here is what I have now.

DateTime varDate;
varDate = dateTimePicker1.Value.Date;
varDate.ToShortDateString();
//string.Format("mm-dd-yyyy");

string dirpath = string.Format(@"\\100-nuweb-k-006.adcs.net\logs\{0:mm-dd-yyyy}\textBox1-Log-Files", dateTimePicker1.Value.Date);

Directory.CreateDirectory(dirpath);

My issue now is that it is showing "00" as the month value from the datetimepicker. How can I fix this>?


Report •

#8
November 6, 2012 at 08:24:51
I got it, mm=minutes MM=months. :)

Report •

#9
November 6, 2012 at 11:19:45
I have a similar issue now, but I am trying to map a network drive with a variable. In the textbox an ip address will be entered. Here is my code so far

string stationpath = string.Format(@"\\"+textBox1.Text+"\\c$\logs", textBox1.Text);
//stationpath.ToString();

IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("S:",stationpath, Type.Missing, "admin", "Password");

I'm getting an network path was not found error. :(


Report •

#10
November 6, 2012 at 11:35:12
Presumably because the server doesn't have a shared called c$<invalid escape sequence>ogs.

How To Ask Questions The Smart Way


Report •

#11
November 6, 2012 at 11:38:31
i have tried changing C$ to C: but have still had no luck.

Report •

#12
November 6, 2012 at 11:43:47
No, I wouldn't imagine you would with that.

C$ is a share.
C$<invalid escape sequence>ogs (what you've shown here) is not a share.
C$\logs is not a share.
C:<invalid escape sequence>ogs (what I presume you're now doing) is not a share.

How To Ask Questions The Smart Way


Report •

#13
November 6, 2012 at 11:56:06
I understand now! Thank You Again!

Report •


Ask Question