VS2015 C# Directory操作文件夹的个别问题!

NieC2018 2018-07-03 07:11:47
C#中利用Directory类中的方法创建文件夹时,有什么办法可以让新建的这个文件夹带上“某种标记“,不是通过特定的文件夹名称来识别,也不是通过文件夹里面的文件内容来识别。就是这个文件夹本身自带的”某种属性“。

我主要是想实现这样一个功能:先创建文件夹,然后在TreeView控件中显示出来,其实本质都是一样的文件夹,但是想显示出来的不一样。不想通过我上面说的那两种方法。所以想创建的时候就做上标记,显示的时候根据标记来显示。

本人C#刚入门,希望各位前辈多多指导,谢谢!(如果表达的有问题,请及时提出,我改正。)
...全文
707 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
richiejen_555 2018-07-05
  • 打赏
  • 举报
回复
谢谢分享 谢谢
ytdingliming 2018-07-05
  • 打赏
  • 举报
回复
学习,谢谢分享
qq_18435011 2018-07-05
  • 打赏
  • 举报
回复
qq_40075532 2018-07-05
  • 打赏
  • 举报
回复
谢谢分享 谢谢
正怒月神 2018-07-04
  • 打赏
  • 举报
回复
设置文件摘要:http://www.cnblogs.com/king_astar/archive/2007/03/08/668332.html
读取文件摘要:百度 shell32
  • 打赏
  • 举报
回复
引用 14 楼 NieC2018 的回复:
你是使用自己的treeview的,文件夹列表你可以放在注册表里可以放在你自己的配置文件临时库里面,这都可以啊,为什么要放在文件夹本身里面?

那么有没有放在文件夹本身里面的存储呢?有,但是文件夹属性并不够用,一般软件(svn、git等)都是在文件夹中新建一个隐藏文件夹实现的。


那我可不可以这样:每次新建文件夹的时候,往文件里放一个配置文件。然后TreeView显示的时候,根据配置文件里面不同的属性,来显示不同的外观。[/quote]

你看看这篇文章跟着做就行了,要注意小图标和大图标一共有很多个,你做的ico文件也要有这么多个,一般是从16x16一直到512x512
  • 打赏
  • 举报
回复
引用 14 楼 NieC2018 的回复:
你是使用自己的treeview的,文件夹列表你可以放在注册表里可以放在你自己的配置文件临时库里面,这都可以啊,为什么要放在文件夹本身里面?

那么有没有放在文件夹本身里面的存储呢?有,但是文件夹属性并不够用,一般软件(svn、git等)都是在文件夹中新建一个隐藏文件夹实现的。


那我可不可以这样:每次新建文件夹的时候,往文件里放一个配置文件。然后TreeView显示的时候,根据配置文件里面不同的属性,来显示不同的外观。[/quote]
可以啊windows也是这么做的你看迅雷的文件夹图标是不是和别人不一样?
zj_zwl 2018-07-04
  • 打赏
  • 举报
回复
文件夹没你有说的可以放自定义属性的地方,你可以:
1.在文件夹内放一个配置文件
2.用数据库记录对应文件夹的自定义数据
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
谢谢各位帮我解决问题,由于本人刚入门,所能理解的东西有限,所以沟通起来比较困难,如果沟通过程中有误解,希望各位谅解!我还是希望能把这个问题搞清楚,谢谢大家!
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
你是使用自己的treeview的,文件夹列表你可以放在注册表里可以放在你自己的配置文件临时库里面,这都可以啊,为什么要放在文件夹本身里面?

那么有没有放在文件夹本身里面的存储呢?有,但是文件夹属性并不够用,一般软件(svn、git等)都是在文件夹中新建一个隐藏文件夹实现的。[/quote]

那我可不可以这样:每次新建文件夹的时候,往文件里放一个配置文件。然后TreeView显示的时候,根据配置文件里面不同的属性,来显示不同的外观。
  • 打赏
  • 举报
回复
引用 12 楼 NieC2018 的回复:
[quote=引用 11 楼 yenange 的回复:]
[quote=引用 10 楼 NieC2018 的回复:]
[quote=引用 6 楼 yenange 的回复:]
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。


您给的代码:是通过不同的按钮赋予了节点不同的“属性”来实现的。我要的效果的是:TreeView只负责在某个路径下找文件夹或是文件,根据文件夹或是文件本身自带的“属性或是标签”来显示不同的外观。[/quote]
新建文件夹, 在哪里新建呢?不是凭白无故的吧?
其实真实情况, 你要做的第一步就是把你想在建立文件夹的那个位置的磁盘目录用树给显示出来。
第二步才是新建文件夹。新建文件夹可以在树节点上右键(当然, 先选中文件夹节点, 再点按钮也行)。
新建文件夹, 你必然是知道这个文件夹是新的, 改对应节点的外观就行, 有什么问题?
难道你还想把“新”的文件夹保存下来, 下次再打开时还能看到这个“新”文件夹是不同?[/quote]

好像有点懂了!我可不可以这样理解:就是我用C#在电脑的磁盘里面创建文件夹的时候,也可以为这个文件夹指定不同的Image属性吗?不是Windows默认的吗?不知道理解的对不对?

是的,我所有通过TreeView创建的文件夹或是文件都是需要保存在电脑里面的。除非我自己删除,否则就一直存在。[/quote]
你是使用自己的treeview的,文件夹列表你可以放在注册表里可以放在你自己的配置文件临时库里面,这都可以啊,为什么要放在文件夹本身里面?

那么有没有放在文件夹本身里面的存储呢?有,但是文件夹属性并不够用,一般软件(svn、git等)都是在文件夹中新建一个隐藏文件夹实现的。
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 11 楼 yenange 的回复:
[quote=引用 10 楼 NieC2018 的回复:]
[quote=引用 6 楼 yenange 的回复:]
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。


您给的代码:是通过不同的按钮赋予了节点不同的“属性”来实现的。我要的效果的是:TreeView只负责在某个路径下找文件夹或是文件,根据文件夹或是文件本身自带的“属性或是标签”来显示不同的外观。[/quote]
新建文件夹, 在哪里新建呢?不是凭白无故的吧?
其实真实情况, 你要做的第一步就是把你想在建立文件夹的那个位置的磁盘目录用树给显示出来。
第二步才是新建文件夹。新建文件夹可以在树节点上右键(当然, 先选中文件夹节点, 再点按钮也行)。
新建文件夹, 你必然是知道这个文件夹是新的, 改对应节点的外观就行, 有什么问题?
难道你还想把“新”的文件夹保存下来, 下次再打开时还能看到这个“新”文件夹是不同?[/quote]

好像有点懂了!我可不可以这样理解:就是我用C#在电脑的磁盘里面创建文件夹的时候,也可以为这个文件夹指定不同的Image属性吗?不是Windows默认的吗?不知道理解的对不对?

是的,我所有通过TreeView创建的文件夹或是文件都是需要保存在电脑里面的。除非我自己删除,否则就一直存在。
吉普赛的歌 2018-07-04
  • 打赏
  • 举报
回复
引用 10 楼 NieC2018 的回复:
[quote=引用 6 楼 yenange 的回复:]
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。


您给的代码:是通过不同的按钮赋予了节点不同的“属性”来实现的。我要的效果的是:TreeView只负责在某个路径下找文件夹或是文件,根据文件夹或是文件本身自带的“属性或是标签”来显示不同的外观。[/quote]
新建文件夹, 在哪里新建呢?不是凭白无故的吧?
其实真实情况, 你要做的第一步就是把你想在建立文件夹的那个位置的磁盘目录用树给显示出来。
第二步才是新建文件夹。新建文件夹可以在树节点上右键(当然, 先选中文件夹节点, 再点按钮也行)。
新建文件夹, 你必然是知道这个文件夹是新的, 改对应节点的外观就行, 有什么问题?
难道你还想把“新”的文件夹保存下来, 下次再打开时还能看到这个“新”文件夹是不同?
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 6 楼 yenange 的回复:
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。


您给的代码:是通过不同的按钮赋予了节点不同的“属性”来实现的。我要的效果的是:TreeView只负责在某个路径下找文件夹或是文件,根据文件夹或是文件本身自带的“属性或是标签”来显示不同的外观。
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 7 楼 yenange 的回复:
using System;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.ImageList = imageList1;
}

private void button1_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("普通节点", 0, 0));
}

private void button2_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("新建节点", 1, 1));
}
}
}


namespace WindowsFormsApp2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(52, 33);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(219, 289);
this.treeView1.TabIndex = 0;
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "img0.png");
this.imageList1.Images.SetKeyName(1, "img1.png");
//
// button1
//
this.button1.Location = new System.Drawing.Point(337, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 23);
this.button1.TabIndex = 1;
this.button1.Text = "添加普通节点";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(337, 149);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "添加新节点";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(491, 401);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}






真的非常感谢,感谢您非常有耐心帮助我解决问题,谢谢!
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 6 楼 yenange 的回复:
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。


您好,您说的这个我知道。
首先我的TreeView的所有节点都是电脑某个盘里面某个路径下的所有文件夹或是文件的映射。那么前提就必须在这个路径下有文件夹或是文件,那么我的理解:
第一步:就要创建文件夹或是文件。
第二步:根据已有的文件夹或是文件,相对应的在Treeview显示出来。(您说对:我要的只是外观效果,其实本质都是文件夹!),既然要显示不同的外观效果,肯定要找不同点。所以在创建这个文件夹或是文件的时候就给他贴上“标签”,然后在TreeView显示的时候,根据不同的“标签”显示不同的外观。

我不知道是不是我哪里理解的有问题!总感觉您跟2#的版主所说的都是第二步要做的事情,不是第一步要做的事情。如果说错话请不要怪罪,是不是我哪里没转过弯来。
吉普赛的歌 2018-07-04
  • 打赏
  • 举报
回复
using System;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.treeView1.ImageList = imageList1;
}

private void button1_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("普通节点", 0, 0));
}

private void button2_Click(object sender, EventArgs e)
{
this.treeView1.Nodes.Add(new TreeNode("新建节点", 1, 1));
}
}
}


namespace WindowsFormsApp2
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region Windows 窗体设计器生成的代码

/// <summary>
/// 设计器支持所需的方法 - 不要修改
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.treeView1 = new System.Windows.Forms.TreeView();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.Location = new System.Drawing.Point(52, 33);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(219, 289);
this.treeView1.TabIndex = 0;
//
// imageList1
//
this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
this.imageList1.Images.SetKeyName(0, "img0.png");
this.imageList1.Images.SetKeyName(1, "img1.png");
//
// button1
//
this.button1.Location = new System.Drawing.Point(337, 96);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(92, 23);
this.button1.TabIndex = 1;
this.button1.Text = "添加普通节点";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(337, 149);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "添加新节点";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(491, 401);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.treeView1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}

#endregion

private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}




吉普赛的歌 2018-07-04
  • 打赏
  • 举报
回复
win form 的 TreeView 还是比较简单的,
在 Form Load 时就指定 imageList ;
每个节点创建时可以指定用哪张图片, 你不过就是一个外观效果, Tag 都可以省了。
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 3 楼 yenange 的回复:
你是 B/S 还是 C/S ? C/S 的话 #2 版主的就可以了


您好,谢谢您的回答。

您说的B/S、C/S我百度了一下,大概了解了一下。我就是用VS2015 -> C# -> WinForm ->实现设备的上位操作软件(也可以说“设备操作窗口”)。
NieC2018 2018-07-04
  • 打赏
  • 举报
回复
引用 2 楼 caozhy 的回复:
利用treenode的 tag 属性保存。或者存入文件/配置文件


您好,谢谢版主的回复。
1、我觉的您说的“treenode的 tag 属性保存”是后面要做的事情,首先是创建文件夹,让文件夹带有可以“设置的特殊属性”。然后才是根据文件夹不同的属性去设置相对应treenode的 tag 属性。不知道我理解对不对,如果说错了,请谅解!

2、“或者存入文件/配置文件”我的理解是:那就还是要进入文件夹里面,看里面的文件或是配置文件来判断此文件夹跟其他文件夹不同?如果我理解错了,请谅解。

3、我有想过通过FileAttribute里面的属性来实现,里面有个Device属性或者其他属性,找不到具体一点的解释,不知道怎么用!
加载更多回复(4)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧