Click here to Skip to main content
16,021,765 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a file to a specific location with the datetime included in the file name. After the file is written, I am needing to prompt a save as box to the user. However, when the program tries to find the newly created file, it is unable to because the datetimes do not match.

I know why it is doing that, I just can't figure out how get around it. I have tried using variables but just can't seem to get it right.

Thank you in advance for any advice or examples.

C#
if (dr[12].ToString().Contains("99|"))
                            {
                                StreamWriter sw = new StreamWriter(@"C:\inetpub\temp\99-Authorize.Net - " + date1 + " @ " + date2 + ".ach");//
                                if (dr[0].ToString() != "2")
                                {
                                    if (dr[9].ToString() != "Total Amount")
                                    {
                                        sw.Write("6                            ");
                                        sw.Write(dr[9].ToString().Replace(".", "").Replace(",", "").PadLeft(10, '0'));
                                        sw.Write((dr[12].ToString()).PadRight(15, ' '));
                                        sw.Write((dr[13].ToString() + " " + dr[14].ToString()).PadRight(40));
                                        sw.Write(sw.NewLine);
                                    }
                                }
                                sw.Write("9");
                                sw.Write(sw.NewLine);
                                sw.Close();


C#
string fileName2 = ("99-Authorize.Net - " + date1 + " @ " + date2 + ".ach");//
        string filePath2 = (@"C:\inetpub\temp\99-Authorize.Net" + date1 + " @ " + date2 + ".ach");

        Response.Clear();
        Response.AppendHeader("content-disposition",
        "attachment; filename=" + fileName2);
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(filePath2);
        Response.Flush();
        Response.End();
Posted

1 solution

Perhaps you could store the file name in a session variable and pass that along.

Something like:

C#
string filename = "99-Authorize.Net - " + date1 + " @ " + date2 + ".ach";
Session.Add("DatedFileName", filename);


You could then use this with:

C#
filename = Session("DateFileName");


Very simplistic but might suit your need.
 
Share this answer
 
Comments
Maciej Los 12-Jun-12 13:02pm    
Nice! +5
Richard C Bishop 12-Jun-12 14:35pm    
Thank you, that is what I was trying to do!
Jani Giannoudis 13-Jun-12 3:03am    
Depending oh the culture, the DateTime.ToString can be conflicting for a file name. You may use string filename = string.Format( "99-Authorize.Net - {0} @ {1}", date1.ToString( "yyyyMMddHHmmss" ), date2.ToString( "yyyyMMddHHmmss" ) );
R. Giskard Reventlov 13-Jun-12 10:24am    
Good addition.
Richard C Bishop 14-Jun-12 9:35am    
That is better code than what I have implemented. Thank you for the insight.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900