怎么实现文件的自动导入数据库,并建立日志文件,看该文件是否导入成功.导入之后把文件自动删除,不成功则不删除~~?有相关例子么?
解决方案 »
- 问个小问题,我发誓绝对简单
- datalist不稳定翻页问题
- 用户登陆
- 各位大哥还记得自己面试时,公司出的技术题目不?(ASP.NET)
- 随便整了个站,欢迎大家来用用。www.baidu10.com 顶就给分。好意见给高分哦。
- VS2005中的一个问题:WEBFORM上拖放的控件,不能自由拖动。如何解决??
- 我通过路由器上网,我如何获取我现在的上网IP呀??
- gdi+ 中发生一般性错误 save()
- E百分学习机的密码忘记了怎么办?谢谢!
- 问个傻傻的问题,在ASP里单引号的问题
- 为什么我的示范程序在本机上无法写入数据库????
- 关于生成静态新闻页问题,请高手给提示一下?急用!!在线等待中.....
是监视的意思么?
那可能要用到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());
}
}
}