如何使用C#打开文件/目录 [英] How to open file/directory using C#

查看:188
本文介绍了如何使用C#打开文件/目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在努力打开列表框中列出的所选文件和目录。



实际上,我设计了列出了C盘上所有文件和目录的列表框。

现在点击一个按钮,使用c#打开所选文件或目录。

请建议。

解决方案

< blockquote> 1)你可以得到这样的所有司机:



string [] drivers = Directory.GetLogicalDrives();



2)你可以提交你的列表框:

string [] dir = Directory.GetFiles(string path);

string [] files = Directory.GetDirectories(string path);



现在你可以使用:

-fileInfo

-directoryInfo



string [] dir = Directory.GetFiles(c:/);

DirectoryInfo dInfo = new DirectoryInfo(c:/ );

FileInfo [] fInfo = dInfo.GetFiles();



- 更多道具:



fInfo [0] .Name;

fInfo [0] .DirectoryName;

fInfo [0] .LastWriteTime;



3)然后你可以填写文件或文件夹名称中的列表框

并使用名称文件来导航在文件


不幸的是,你的问题是基于对文件系统做的一些误解(实际上,以及你以前的问题)。请看我对这个问题的评论。



涉及几个不同的类和方法,具体取决于你要对文件做什么。首先, System.IO.File

http://msdn.microsoft.com/en-us/library/system.io.file.aspx [ ^ ]。



现在,打开一个 System.IO.StreamReader System.IO.StreamWriter 级别的文件,或 System.IO.BinaryReader 。这些类在使用中最实用和最强大。请参阅:

http://msdn.microsoft.com /en-us/library/vstudio/system.io.streamreader [ ^ ],

http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter [ ^ ],

http://msdn.microsoft.com/en-us/library/vstudio/system.io.binaryreader [ ^ ],

http://msdn.microsoft.c om / en-us / library / vstudio / system.io.binarywriter [ ^ ]。



您还可以使用系统打开文件.IO.FileStream

http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx [ ^ ]。



我不确定它可以帮到你除非你最终了解文件系统的功能并获得基本概念。很难帮助那些打算打开目录的人。也许,你的误解与你对某些软件应用程序的观察有关,这些软件应用程序可以直观地打开目录,但是根据它背后发生的事情来整理UI非常重要。



-SA


Hi,

Am struggling in open the selected file and directory listed on listbox.

Actually, I have designed the listbox which list all files and directory present on C drive.
Now a button click which has to open the selected file or directory using c#.
Please advice.

解决方案

1) you can get all drivers like this:

string[] drivers = Directory.GetLogicalDrives();

2)you can file your list box:
string[] dir = Directory.GetFiles(string path);
string [] files= Directory.GetDirectories(string path);

now you can use in this:
-fileInfo
-directoryInfo

string[] dir = Directory.GetFiles("c:/");
DirectoryInfo dInfo = new DirectoryInfo("c:/");
FileInfo[] fInfo= dInfo.GetFiles();

- alot more prop:

fInfo[0].Name;
fInfo[0].DirectoryName;
fInfo[0].LastWriteTime;

3) and then you can fill the list boxs in the files or folder name
and use the name file to Navigate in files


Unfortunately, your question is based on some misconceptions on what file systems do (actually, as well as your previous question). Please see my comments to the question.

Several different classes and methods are involved, depending on what you are going to do with the file. First of all, System.IO.File:
http://msdn.microsoft.com/en-us/library/system.io.file.aspx[^].

Now, opening a file at the level of System.IO.StreamReader, System.IO.StreamWriter, or System.IO.BinaryReader. These classes are most practical and robust in use. Please see:
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamreader[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.io.streamwriter[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.io.binaryreader[^],
http://msdn.microsoft.com/en-us/library/vstudio/system.io.binarywriter[^].

And also you can open the file with the System.IO.FileStream:
http://msdn.microsoft.com/en-us/library/system.io.filestream.aspx[^].

I am not sure it can help you, unless you finally learn what file systems do and get the basic concepts. It's hard to help someone who is aiming to "open" a directory. Perhaps, you misconception is related to your observation of some software application which "open" the directory visually, but it's very important to sort out UI from what happens behind it.

—SA


这篇关于如何使用C#打开文件/目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆