- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
最后,我编写了自动更新的代码。文件被添加到数据库中。但我无法更新第 0 列中的文件名。它只给出了第一次文件名。当我将文件名更改为其他名称时,它不会在 column0 中给出。
我只需要在部分
中输入特定的文件名。我不知道如何修剪文件名并在 column0 中给出。请帮帮我。
此外,我发现了一个重要的错误。当我使用按钮将文件从单元格 [1] 移动到单元格 [2] 时。整个文件被移动到单元格 [2]。所以那个时候文件观察者正在寻找那个特定的文件夹并理解,文件被删除了..所以代码将删除数据库..我不想那样做..我该如何处理这种情况..
请帮帮我
我的自动更新代码片段:
namespace shell_FileSystemWatcher
{
public partial class Form1 : Form
{
public string partKey = "";
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Create a new object of FileSystemWatcher
/// </summary>
FileSystemWatcher watcher = new FileSystemWatcher();
/// <summary>
/// Initialize the
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
ListBox listbox1 = new ListBox();
private void Form1_Load_1(object sender, EventArgs e)
{
///Creating listbox in current form
this.Controls.Add(listbox1);
listbox1.Size = new Size(500, 200);
listbox1.Location = new Point(0, 0);
listbox1.Visible = true;
///Assigning some properties to FileSystemWatcher class object
///Assign the path
watcher.Path = @"C:\user\elec\copy";
///Assign the filters as your requirement
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName;
///Handle the events that will be called when any file will be changed in the given folder path.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
///Enabling the event call
watcher.EnableRaisingEvents = true;
///Initializing delegate that we have created to update UI control outside the current thread
addItemInList = new delAddItem(this.AddString);
}
// Define the event handlers.
private void OnChanged(object source, FileSystemEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
switch (e.ChangeType)
{
case WatcherChangeTypes.Created:
//Insert file in database
this.Invoke(addItemInList, "File: " + e.FullPath + " Created");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"INSERT INTO cncinfo (part,draftpath) VALUES ('" + file.Name + "','" + e.FullPath + "') ", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Deleted:
//remove file from database
this.Invoke(addItemInList, "File: " + e.FullPath + " Deleted");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"delete cncinfo where part='" + file.Name + "' ;", con);
cmd.ExecuteNonQuery();
con.Close();
}
break;
case WatcherChangeTypes.Changed:
///if you are storing file in database(not file name whole file in binary format)
///then you can update the file in database here
///this event will be fired when any data has changed in the file.
this.Invoke(addItemInList, "File: " + e.FullPath + " Changed");
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + NotifyFilters.FileName
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}
break;
}
}
private void OnRenamed(object source, RenamedEventArgs e)
{
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
//Update then old filename with new here
this.Invoke(addItemInList, string.Format("File: {0} renamed to {1}", e.OldFullPath, e.FullPath));
{
SqlConnection con = new SqlConnection(@"Data Source=Stacy492\SQLEXPRESS;Initial Catalog=cndb;Integrated Security=True");
con.Open();
//s.debasish79@gmail.com
SqlCommand cmd = new SqlCommand(@"update cncinfo set part='" + file.Name
+ "',draftpath='" + e.FullPath + "' where part='" + file.Name + "'", con);
cmd.ExecuteNonQuery();
con.Close();
this.Validate();
}
}
#region We are creating delegate to update the value in Windows Form Control
/// <summary>
/// We are using delegate invoke method to update the UI control outside the current thread.
/// otherwise it will throws the error.
/// you can also use this method to update the cell value in datagridview
/// </summary>
/// <param name="_string"></param>
private delegate void delAddItem(string _string);
private delAddItem addItemInList;
private void AddString(string _string)
{
listbox1.Items.Add(_string);
}
#endregion
}
最佳答案
要获取文件名,这将帮助您!!!!
FileInfo file = new FileInfo(e.FullPath);
Console.WriteLine(file.Name);
关于c# - 如何在filewatcher中更新文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715447/
我正在使用 .net filewatcher 监视某个文件夹以获取某些类型的文件 (*.mbxml)。我正在使用 filewatcher 创建的事件。一旦创建的事件触发,我必须将此文件移动到另一个文件
下面是我编写的文件观察器类的代码: class FileWatcher { #region Method that begins watching public static v
我正在构建一个应用程序,我将在其中查看文件。我对编程很陌生,所以我(也许)没有什么问题,因为我没有真正的实践。我的程序可以正常工作,但我不知道在“真实”软件中通常是如何实现的。请概述和评论。因为我喜欢
尝试编译 .less 时我做错了什么文件到 .css使用 PHPStorm 文件观察器? 这是屏幕截图,(请打开 THIS LINK 以查看图像的完整尺寸): 我安装了 npm install -g
在 Java 中,我正在使用介绍的过程 here 。当我与文件交互时一切正常,但在处理文件夹时遇到问题。该代码确实检测到已添加/修改了新文件夹等,但它没有告诉我这是一个文件夹而不是文件。 它会说“fo
我想用 powershell 查看文件夹,并且我是 PS 初学者。 当我启动脚本时,该脚本只能运行一次。 但是当我因为更改了一些脚本代码而必须再次重新启动脚本时,我收到此错误消息: Cannot su
我正在尝试将多线程与java中的FileWatcher服务集成。即,我不断地监听特定目录 -> 每当创建新文件时,我需要生成一个处理该文件的新线程(假设它打印文件内容)。我设法编写了一个可以编译并运行
我制作了一个 FileWatcher,但我的 FileWatcher 没有按计划工作,我卡住了。 我想要一个可以处理 2 个 map 的文件观察器。 我自己做的代码不会选择我说他应该选择的路径。 在我
我想知道是否在给定目录中创建了新文件。我有下一个代码: private static void CreateWatcher() { //Create a new FileSys
我在我的 winform 应用程序中使用 filewatcher 控件。我正在通过处理其事件来执行上传任务。 现在的问题是,当我在目录(在 FileWatcher 控制下监视)中创建任何新文档时,上传
我是 Rx 的新手,希望在我当前的项目中使用它。我正在尝试实现一个文件观察器系统。至少现在我只对文件创建事件感兴趣。但是当我试图在代码下面运行时,我收到“值不能为空错误消息”。请有人帮我解决以下代码。
这是来自 FileWatcher java 7 nio 库的代码片段。它是非阻塞代码吗?该线程等待来自文件系统的信号。 for (;;) { // wait for key to be sig
我想在 IntelliJ 中使用 SCSS 文件。我看到 tutorial我试着跟着它。但出于某种原因,这对我不起作用。 我做了什么: (我有一台mac电脑)我下载节点 sass 库:npm inst
第一次发布长期读者。 我在 Windows 窗体应用程序中构建了一个工作文件观察器,在将其移动到 Windows 服务之前 100% 正常运行,现在我收到两个单独的问题。此文件观察器读取平面文件以进行
我正在使用 .net 2.0 filewatcher 来监视文件夹中的新文件。它工作得很好,除非我一次放置超过 80 个文件。该事件不再触发。就好像 filewatcher 被设置为跟踪一定数量的文件
我在尝试使用 WebStorm LESS File Watcher 时遇到此错误 /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/
在使用 WPF 应用程序时,我遇到了一个非常疯狂的问题,该应用程序使用 Singleton 实例模式来确保只有一个实例在运行。单个实例检测和命令行转发机制工作正常,但是,作为在辅助实例上退出的启动代码
几天前我开始使用 Rider,我想开始在我的元素中使用 SCSS 并将它们编译回 CSS,我正在摆弄文件观察器,但我无法让它工作,我一直收到这个当我将“显示控制台”设置为“始终”时,控制台中会显示消息
几天前我开始使用 Rider,我想开始在我的元素中使用 SCSS 并将它们编译回 CSS,我正在摆弄文件观察器,但我无法让它工作,我一直收到这个当我将“显示控制台”设置为“始终”时,控制台中会显示消息
我开发了一个filewatcher程序来监控一个文件夹,如果文件有任何变化,它会把文件复制到另一个文件夹。 但是我发现在写入原始文件时会出现错误信息(例如文件正在被另一个应用程序处理......)似乎
我是一名优秀的程序员,十分优秀!