当前位置:编程学习 > C#/ASP.NET >>

拷贝文件的一点问题

source=@"\\192.168.0.1\a\b\1.gif";
string=@"a\b\1.gi";
File.Copy(source,strimg);

因为目标的文件夹还没有建立,所以出错,请问怎么做最简单 --------------------编程问答-------------------- 先判断文件夹有没有存在,不存在则先建文件夹,反之则copy
if (!Directory.Exists(FolderPath))
            {
                //文件夹不存在则创建
                Directory.CreateDirectory(FolderPath);
                //写入日志
                string FilePath = Server.MapPath("/LoginLog/" + DateTime.Now.ToString("yyyy-MM") + "/" + DateTime.Now.ToString("yyyy-MM-dd"));
                if (!Directory.Exists(FilePath))
                {
                    //如果文件存在则写入日志
                    using (StreamWriter writer = File.AppendText(FilePath + ".txt"))
                    {
                        writer.WriteLine("[登录成功!][用户IP]:" + ip + "; [登录时间]:" + DateTime.Now);
                        writer.Flush();
                        writer.Close();
                    }
                }
                else
                {
                    //如果文件不存在当前目录下创建一个文本文件  
                    StreamWriter writer = new StreamWriter(FilePath + ".txt");

                    writer.WriteLine("[登录成功!][用户IP]:" + ip + "; [登录时间]:" + DateTime.Now);
                    writer.Flush();
                    writer.Close();
                }
            }
            else
            {
                //文件夹存在则写入日志
                string FilePath = Server.MapPath("/LoginLog/" + DateTime.Now.ToString("yyyy-MM") + "/" + DateTime.Now.ToString("yyyy-MM-dd"));
                if (!Directory.Exists(FilePath))
                {
                    //如果文件存在则写入日志
                    using (StreamWriter writer = File.AppendText(FilePath + ".txt"))
                    {
                        writer.WriteLine("[登录成功!][用户IP]:" + ip + "; [登录时间]:" + DateTime.Now);
                        writer.Flush();
                        writer.Close();
                    }
                }
                else
                {
                    //如果文件不存在当前目录下创建一个文本文件  
                    StreamWriter writer = new StreamWriter(FilePath + ".txt");

                    writer.WriteLine("[登录成功!][用户IP]:" + ip + "; [登录时间]:" + DateTime.Now);
                    writer.Flush();
                    writer.Close();
                }
            }
            Response.Redirect("ManageLogin/");
        } --------------------编程问答-------------------- 加入ASP。NET C#群吧,群号是:  1873156
补充:.NET技术 ,  C#
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,