Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java File Object equivalent in C#

Tags:

java

c#

file

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?

like image 894
lordscales91 Avatar asked Apr 29 '26 09:04

lordscales91


1 Answers

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;
    }
}
like image 142
John Jeheimer Avatar answered Apr 30 '26 21:04

John Jeheimer