简体   繁体   中英

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#.

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:

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#? 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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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