FileSystemWatcher怎么取得监控目录名 本帖最后由 mpy2003 于 2013-11-11 13:05:17 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 目前我只能这样重新过一遍,不知道有没有更好的方法private void watcher_Created(object sender, FileSystemEventArgs e){ foreach (ListViewItem i in this.listView.Items) { if (e.FullPath.ToString().IndexOf(i.SubItems[2].Text) > -1) { string dataName = i.Text; } }} private void watcher_Created(object sender, FileSystemEventArgs e){ Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);} 分拆全路径。参考如果字符串格式为String str="abc#def#hijkl#mn";string[] s = str.Split(new char[] { '#' });结果就是s[0]="abc";s[1]="def";s[2]="hijkl";s[3]="mn"; private void watcher_Created(object sender, FileSystemEventArgs e){ string strDirectoryName = Path.GetDirectoryName(e.FullPath); this.textBox1.Text = strDirectoryName;} 假如设置路径是Path=C:\a\b得到的e.FullPath=C:\a\b\c\d\e.txt没有这种属性可以知道是Path=C:\a\b吗? string a = @"C:\a\b\c\d\e.txt"; FileInfo fileInfo = new FileInfo(a); Console.WriteLine(fileInfo.Directory.Parent.Parent.FullName);//输出C:\a\b 据说IO下有个类叫Path.....据说他能分析你的路径。你要做的只是把路径赋给Path this.fileSystemWatcher1.Path =@“C:\a\b”;e.FullPath=C:\a\b\c\d\e.txt,string strDirectoryName = Path.GetDirectoryName(e.FullPath);返回就是C:\a\b\c\d private void watcher_Created(object sender, FileSystemEventArgs e){ var path = (sender as FileSystemWatcher).Path;} 你本身默认的路径是this.fileSystemWatcher1.Path =@“C:\a\b”,下面还有子目录,要取主目录,用this.fileSystemWatcher1.Path就可阿, 貌似不知道有多少级的子目录时,这个不太方便用[/quote=引用 10 楼 starfd 的回复:]据说IO下有个类叫Path.....据说他能分析你的路径。你要做的只是把路径赋给Path不明白具体操作你没有明白我的意思再说一下我的意思:假如设置路径是Path=C:\a\b 监控得到的e.FullPath=C:\a\b\e.txt监控得到的e.FullPath=C:\a\b\c\e.txt监控得到的e.FullPath=C:\a\b\c\d\e.txt监控得到的e.FullPath=C:\a\b\c\d\e\f.txt不管路径是什么样的,有多少级目录最后得到监控路径Path=C:\a\b 那是不是可以获取指定级数的目录呢不管你得到是C:\a\b\e.txt,C:\a\b\c\e.txt你都只取两级目录得到C:\a\b 这是应该是可以的只要你得到的对应的FileSystemWatcher,并结合FileSystemEventArgs,可以拿到你想要的任何数据,如果不行,你甚至可以写个新类,继承FileSystemWatcher,并添加一些属性,来记录你想要的数据这么多楼了,楼主整理下,这个应该是可以解决的 string path = (sender as FileSystemWatcher).Path.ToString(); C#的textbox如何透明? C#中的注释问题 想学习ORM,谁能用一个最简单的例子给我上一课? c# winform如何实现快截键? C#创建文件的问题 有难度:固定输入法 绘图方面的问题,先谢谢了!! DataTable.Select能否使用top operator? 各位,请问这个SQL语句是什么意思啊? 该进程或线程自上一个步骤以来已更改的问题,求助求助 大叔大婶们,请问:如何动态修改datagridview中的数据啊? 求高手请教定义数组 sQL数据库连接
private void watcher_Created(object sender, FileSystemEventArgs e)
{
foreach (ListViewItem i in this.listView.Items)
{
if (e.FullPath.ToString().IndexOf(i.SubItems[2].Text) > -1)
{
string dataName = i.Text;
}
}
}
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
如果字符串格式为
String str="abc#def#hijkl#mn";
string[] s = str.Split(new char[] { '#' });
结果就是
s[0]="abc";
s[1]="def";
s[2]="hijkl";
s[3]="mn";
private void watcher_Created(object sender, FileSystemEventArgs e)
{
string strDirectoryName = Path.GetDirectoryName(e.FullPath);
this.textBox1.Text = strDirectoryName;
}
得到的e.FullPath=C:\a\b\c\d\e.txt
没有这种属性可以知道是Path=C:\a\b吗?
FileInfo fileInfo = new FileInfo(a);
Console.WriteLine(fileInfo.Directory.Parent.Parent.FullName);//输出C:\a\b
你要做的只是把路径赋给Path
e.FullPath=C:\a\b\c\d\e.txt,
string strDirectoryName = Path.GetDirectoryName(e.FullPath);
返回就是C:\a\b\c\d
{
var path = (sender as FileSystemWatcher).Path;
}
[/quote=引用 10 楼 starfd 的回复:]
据说IO下有个类叫Path.....据说他能分析你的路径。
你要做的只是把路径赋给Path
不明白具体操作
你没有明白我的意思再说一下我的意思:
假如设置路径是Path=C:\a\b
监控得到的e.FullPath=C:\a\b\e.txt
监控得到的e.FullPath=C:\a\b\c\e.txt
监控得到的e.FullPath=C:\a\b\c\d\e.txt
监控得到的e.FullPath=C:\a\b\c\d\e\f.txt
不管路径是什么样的,有多少级目录
最后得到监控路径Path=C:\a\b
不管你得到是C:\a\b\e.txt,C:\a\b\c\e.txt
你都只取两级目录得到C:\a\b
只要你得到的对应的FileSystemWatcher,并结合FileSystemEventArgs,可以拿到你想要的任何数据,如果不行,你甚至可以写个新类,继承FileSystemWatcher,并添加一些属性,来记录你想要的数据这么多楼了,楼主整理下,这个应该是可以解决的