none
Set Variable Equal To CreateDirectory() RRS feed

  • Question

  • I am creating Directories based off variables values, but I want to assign a variable to the creation commands.

    Example -> If I am using this command to actually create the directory

    string localDefaultDir = @"C:\\Testing\\";
    string name = "George";
    
    Directory.CreateDirectory(localDefaultDir + name);

    Is there a way that I can then set a variable named string George = Directory.CreateDirectory(localDefaultDir + name);

    I have tried many ways, but I get multiple compile errors.  There must be a way to achieve what I am after!!!

    Friday, August 17, 2018 1:45 PM

Answers

  • private static void CreateDirAndDownload ( string name )
    {
       var fullPath = Path.Combine(localDefaultDir, name);
    
       Directory.CreateDirectory(fullPath);
       foreach (var file in files)
          using (var output = File.OpenWrite(Path.Combine(fullPath, file.Name))
          {
             sftp.DownloadFile(RemoteDir + remoteFileName, output);
          };
    }
    



    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Friday, August 17, 2018 2:51 PM
    Friday, August 17, 2018 2:49 PM
    Moderator

All replies

  • What is your purpose of setting a variable to a method call? Can you execute it? Yes via reflection. Does it make sense, not really. Please explain why you are trying to assign a runtime method call to a variable. 

    Michael Taylor http://www.michaeltaylorp3.net

    Friday, August 17, 2018 2:10 PM
    Moderator
  • What is your purpose of setting a variable to a method call? Can you execute it? Yes via reflection. Does it make sense, not really. Please explain why you are trying to assign a runtime method call to a variable. 

    Michael Taylor http://www.michaeltaylorp3.net

    A step further on the above code - this is more syntax, but I am actually creating about 5 Directories based off values returned from a database.  I was trying to just set a variable equal to the Directory Creation so I could do 

    private const string localDefaultDir = @"C:\\Testing\\";
    
    static void Main(string[] args)
    {
    	CreateDirAndDownload(dbValues.name);
    }
    struct DBData
    {
    	public string name;
    }
    private static DBData QueryDB()
    {
    	//Do some db calls and return data
    	var dbValues = new DBData
    	{
    		name = nm
    	};
    	return dbValues;
    }
    private static void CreateDirAndDownload(string name)
    {
    	Directory.CreateDirectory(localDefaultDir + name);
    
    	foreach (var file in files)
    	{
    		string remoteFileName = file.Name;
    
    		using (Stream file1 = System.IO.File.OpenWrite(localDefaultDir +"\\"+ name +"\\"+remoteFileName))
    			sftp.DownloadFile(RemoteDir + remoteFileName, file1);
    	}
    }

    (pulling from my OP) instead of the above syntax.

    using (Stream file1 = System.IO.File.OpenWrite(George + "\\"+remoteFileName))

    Friday, August 17, 2018 2:20 PM
  • Still not really sure what you are trying to do, but if you look at the Directory.CreateDirectory method you will see that it returns a DirectoryInfo object, not a string.

    e.g

    DirectoryInfo dirInfo = Directory.CreateDirectory(Path.Combine(localDefaultDir, name));

    The DirectoryInfo object has a variety of methods and properties you can then use to perform further processing on that directory though, again, I'm still not entirely sure what you are trying to do!

    Friday, August 17, 2018 2:35 PM
  • Still not really sure what you are trying to do, but if you look at the Directory.CreateDirectory method you will see that it returns a DirectoryInfo object, not a string.

    e.g

    DirectoryInfo dirInfo = Directory.CreateDirectory(Path.Combine(localDefaultDir, name));

    The DirectoryInfo object has a variety of methods and properties you can then use to perform further processing on that directory though, again, I'm still not entirely sure what you are trying to do!

    Essentially trying to combine these two lines

    	Directory.CreateDirectory(localDefaultDir + name);
    	string dir1 = localdefaultDir + name;


    Friday, August 17, 2018 2:37 PM
  • private static void CreateDirAndDownload ( string name )
    {
       var fullPath = Path.Combine(localDefaultDir, name);
    
       Directory.CreateDirectory(fullPath);
       foreach (var file in files)
          using (var output = File.OpenWrite(Path.Combine(fullPath, file.Name))
          {
             sftp.DownloadFile(RemoteDir + remoteFileName, output);
          };
    }
    



    Michael Taylor http://www.michaeltaylorp3.net

    • Marked as answer by IndigoMontoya Friday, August 17, 2018 2:51 PM
    Friday, August 17, 2018 2:49 PM
    Moderator
  • ?

    You can either do it the other way round (notice I'm using the Path class to combine the two names though, as this will take care of any extraneous or missing backslashes).

    string dir1 = Path.Combine(localdefaultDir, name);
    DirectoryInfo di = Directory.CreateDirectory(dir);
    
    // continue to use dir1 if you just need the use the path again.

    Or read the name back out of the DirectoryInfo object

    DirectoryInfo di = Directory.CreateDirectory(Path.Combine(localdefaultDir, name));
    
    string fullFilePath = Path.Combine(di.FullName, remoteName);

    • Edited by RJP1973 Friday, August 17, 2018 2:50 PM
    Friday, August 17, 2018 2:49 PM