怎么实现文件的自动导入数据库,并建立日志文件,看该文件是否导入成功.导入之后把文件自动删除,不成功则不删除~~?有相关例子么?

解决方案 »

  1.   

    文件自动导入?
    是监视的意思么?
    那可能要用到FileSystemWatcher得到文件后,
    try
    {
         //建立日志文件,就是把你的操作过程写到一个文本文件里,可以看看file类
        //解析文件并且导入数据库,这个根据具体业务逻辑
        //删除文件file.delete
    }
    catch
    {
        //写入错误日志
    }具体的代码我没有
    不过csdn上已经介绍很清楚了下面的代码示例创建一个新文本文件并向其写入一个字符串。
    using System;
    using System.IO;
    public class TextToFile 
    {
        private const string FILE_NAME = "MyFile.txt";
        public static void Main(String[] args) 
        {
            if (File.Exists(FILE_NAME)) 
            {
                Console.WriteLine("{0} already exists.", FILE_NAME);
                return;
            }
            StreamWriter sr = File.CreateText(FILE_NAME);
            sr.WriteLine ("This is my file.");
            sr.WriteLine ("I can write ints {0} or floats {1}, and so on.", 
                1, 4.2);
            sr.Close();
        }
    }
    下面的代码示例打开 log.txt 文件(如果文件不存在则创建文件)以进行输入,并将信息附加到文件尾。然后将文件的内容写入标准输出,以便显示出来。
    using System;
    using System.IO;
    class DirAppend
    {
        public static void Main(String[] args)
        {
            StreamWriter w = File.AppendText("log.txt");
            Log ("Test1", w);
            Log ("Test2", w);
            // Close the writer and underlying file.
            w.Close();
            // Open and read the file.
            StreamReader r = File.OpenText("log.txt");
            DumpLog (r);
        }
        public static void Log (String logMessage, TextWriter w)
        {
            w.Write("\r\nLog Entry : ");
            w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
                DateTime.Now.ToLongDateString());
            w.WriteLine("  :");
            w.WriteLine("  :{0}", logMessage);
            w.WriteLine ("-------------------------------");
            // Update the underlying file.
            w.Flush(); 
        }
        public static void DumpLog (StreamReader r)
        {
            // While not at the end of the file, read and write lines.
            String line;
            while ((line=r.ReadLine())!=null)
            {
                Console.WriteLine(line);
            }
            r.Close();
        }
    }下面的示例从指定的路径删除一个文件。
    using System;
    using System.IO;class Test 
    {
        public static void Main() 
        {
            string path = @"c:\temp\MyTest.txt";
            try 
            {
                using (StreamWriter sw = File.CreateText(path)) {}
                string path2 = path + "temp";            // Ensure that the target does not exist.
                File.Delete(path2);            // Copy the file.
                File.Copy(path, path2);
                Console.WriteLine("{0} was copied to {1}.", path, path2);            // Delete the newly created file.
                File.Delete(path2);
                Console.WriteLine("{0} was successfully deleted.", path2);
            } 
            catch (Exception e) 
            {
                Console.WriteLine("The process failed: {0}", e.ToString());
            }
        }
    }
      

  2.   

    至于FileSystemWatcher,可以看看这里http://www.cnblogs.com/hotcan/articles/29392.html