Hi
I have one specific folder which might be found on two different locations (depends on the operating system that is being used)
I need to set a code which checks those two locations and see if the folder exists in one of them and after that my code will use the path to get the files inside that folder.
Example:
string SourcePath; // It can be @"C:\Program files or @"C:\Documents or settings
so I need to use one single variable and store the paths in in order to use as following:
if (System.IO.Directory.Exists(SourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);
}
Loading
VulpesPosted Aug 19, 2014, 9:20 AM
Eline SamiPosted Aug 20, 2014, 4:56 AM
Akhil KharePosted Aug 19, 2014, 5:49 AM
You are not sure where exactly your folder exist, and its happen in most if the case where you not confirm where exactly Operating System create that particular folder. For all these special folders there is an enum
System.Environment.SpecialFolder
you can use this like
Ankur JainPosted Aug 19, 2014, 5:15 AM
Eline SamiPosted Aug 19, 2014, 4:50 AM
Can you make it more simpler? or if there is another way to store as a string variable?
Ankur JainPosted Aug 19, 2014, 4:12 AM
Dipankar BiswasPosted Aug 19, 2014, 4:11 AM
The following code example demonstrates some of the main members of the Path class.
using System;
using System.IO;
class Test
{
public static void Main()
{
string path1 = @"c:\temp\MyTest.txt";
string path2 = @"c:\temp\MyTest";
string path3 = @"temp";
if (Path.HasExtension(path1))
{
Console.WriteLine("{0} has an extension.", path1);
}
if (!Path.HasExtension(path2))
{
Console.WriteLine("{0} has no extension.", path2);
}
if (!Path.IsPathRooted(path3))
{
Console.WriteLine("The string {0} contains no root information.", path3);
}
Console.WriteLine("The full path of {0} is {1}.", path3, Path.GetFullPath(path3));
Console.WriteLine("{0} is the location for temporary files.", Path.GetTempPath());
Console.WriteLine("{0} is a file available for use.", Path.GetTempFileName());
/* This code produces output similar to the following:
* c:\temp\MyTest.txt has an extension.
* c:\temp\MyTest has no extension.
* The string temp contains no root information.
* The full path of temp is D:\Documents and Settings\cliffc\My Documents\Visual Studio 2005\Projects\ConsoleApplication2\ConsoleApplication2\bin\Debug\temp.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\ is the location for temporary files.
* D:\Documents and Settings\cliffc\Local Settings\Temp\8\tmp3D.tmp is a file available for use.
*/
}
}