Wednesday, 19 June 2013

If File or Directory not exist then create folder and file and if exist then create

string path = "uploads\\investeducationfiles";
                    bool folderExists = Directory.Exists(Server.MapPath(path));
                    if (!folderExists)
                        Directory.CreateDirectory(Server.MapPath(path));

                    string path1 = Server.MapPath("uploads\\investeducationfiles\\log.xls");

                    if (!File.Exists(path1))
                    {
                        using (StreamWriter sw = File.CreateText(path1))
                        {
                            sw.Write(strattechment.ToString());
                        }
                    }
                    else
                    {
                        using (FileStream fs = new FileStream(path1, FileMode.Create))
                        {
                            using (StreamWriter sw = new StreamWriter(fs))
                            {
                                sw.Write(strattechment.ToString());
                            }
                        }
                    }

No comments:

Post a Comment