简体   繁体   English

C#中的Java File Object等价物

[英]Java File Object equivalent in C#

Well, the title says it, I want to know if there is any Object Wrappers equivalent to that in C#. 好吧,标题说,我想知道是否有任何与C#相同的Object Wrappers。

What I want to do is create a sub-directory, inside the parent directory, of a file provided by the user. 我想要做的是在父目录内创建用户提供的文件的子目录。 In Java I would do: 在Java中我会这样做:

JFileChooser chooser=new JFileChooser(new File("."));
chooser.showOpenDialog();
File selectedFile=chooser.getSelectedFile();
File subDir=new File(selectedFile.getParentFile(), "subdir_name");
subDir.mkdir();

What would be the equivalent in C#? C#中的等价物是什么? Or, maybe I need to do a different work-around by using the file path? 或者,也许我需要通过使用文件路径进行不同的解决方案?

Perhaps something like this? 也许是这样的?

String InitialDir = "c:\\";
String DirFilter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

OpenFileDialog myDialog = new OpenFileDialog
{
    InitialDirectory = InitialDir,
    Filter = DirFilter,
    FilterIndex = 2,
    RestoreDirectory = true,
};            

if(myDialog.ShowDialog() == DialogResult.OK)
{
    try
    {
        FileInfo myFile = new FileInfo(myDialog.FileName);
        Directory.CreateDirectory(Path.Combine(myFile.DirectoryName, "subdir_name"));                    
    }
    catch 
    { 
        // exception handling here
        throw;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM