locked
Accessing a folder created by Solution Explorer RRS feed

  • Question

  • Dear Sirs:

     

    In C#, if I use the Solution Explorer to create a new folder; how can I access that folder's contents through code?

     

    I want to be able to copy the contents of this folder to another location on the end user's system using my application, but don't know how to access or refer to this folder in code.

     

    Thank you for your help in this

    Wednesday, April 23, 2008 8:19 PM

Answers

  • A folder created in Solution Explorer is the same as a folder created in Windows Explorer, so you would access it with the same kind of objects you would normally access files and directories with, including DirectoryInfo, FileInfo, Directory and File.  These are all located in the System.IO namespace.

    Wednesday, April 23, 2008 8:29 PM
  • Thursday, April 24, 2008 8:43 PM
  • I regularly use Assembly.GetExecutingAssembly().Location to do this sort of thing... Here's a quick example:

     

     

    Code Snippet

    string exePathAndFilename = Assembly.GetExecutingAssembly().Location;

    string exePath = Path.GetDirectoryName(exePathAndFilename);

    string solutionFolderPath = Path.Combine(exePath, "MyFolderName");

     

     

    GetExecutingAssembly returns a reference to the currently running assembly... i.e. your exe/dll. The Location property on the Assembly class returns the full path and filename for the file the assembly was loaded from. The solution folder you're refering to should be relative to that folder, so just use the normal Path static methods to build that path name.

     

    Unfortunately, none of the folders specified in the Environment.SpecialFolder enumeration will return the location that your program was installed to.

    Thursday, April 24, 2008 10:02 PM

All replies

  • A folder created in Solution Explorer is the same as a folder created in Windows Explorer, so you would access it with the same kind of objects you would normally access files and directories with, including DirectoryInfo, FileInfo, Directory and File.  These are all located in the System.IO namespace.

    Wednesday, April 23, 2008 8:29 PM
  • Hello David:

     

    After my application is installed on the user's system, the App along with the folder I created via Solution Explorer, will reside somewhere in C:\Documents and Settings\their user name\?Local Settings?...

     

    How do I refer to its location in code? I tried creating a method for copying the contents of this folder to a destination folder I create using DirectoryInfo.  However, not knowing the exact path to where my App will get installed with my folder, I tried setting the source path as follows:

     

    string sourceDirectory = @"C:\*.MyDataSource";  However, this threw an exception, as it doesn't like the *. wildcard.

     

    You are right about this folder being visible via Windows Explorer, but as I won't know the exact user name that will exist off of Documents and Settings, how can I programmatically refer to the path for this source folder via C# code?

     

    Don't I need to provide a qualified path to where my App and its folder reside?

     

    Thank you for your assistance.

     

    Thursday, April 24, 2008 8:39 PM
  • Thursday, April 24, 2008 8:43 PM
  • I regularly use Assembly.GetExecutingAssembly().Location to do this sort of thing... Here's a quick example:

     

     

    Code Snippet

    string exePathAndFilename = Assembly.GetExecutingAssembly().Location;

    string exePath = Path.GetDirectoryName(exePathAndFilename);

    string solutionFolderPath = Path.Combine(exePath, "MyFolderName");

     

     

    GetExecutingAssembly returns a reference to the currently running assembly... i.e. your exe/dll. The Location property on the Assembly class returns the full path and filename for the file the assembly was loaded from. The solution folder you're refering to should be relative to that folder, so just use the normal Path static methods to build that path name.

     

    Unfortunately, none of the folders specified in the Environment.SpecialFolder enumeration will return the location that your program was installed to.

    Thursday, April 24, 2008 10:02 PM
  • Hello David:

     

    Sorry I didn't respond sooner, but I've been out of town and just got back.

     

    I wanted to thank you for all your suggestions.

     

    Sincerely

     

    CodeMann

    Monday, May 5, 2008 8:44 PM
  • Hello thoward37:

     

    Sorry I didn't respond sooner, but I've been out of town and just got back.

     

    I wanted to thank you for all your suggestions.

     

    This looks like it will return the path I needed. Thanks

     

    Sincerely

     

    CodeMann

    Monday, May 5, 2008 8:46 PM