I'm trying to access the AppData folder to create/delete directories as needed but using Path.Combine is yielding only half the desired path. Here's what I have:
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string test = Path.Combine(sPath, #"\Microsoft\Windows\Start Menu\SkillControl\");
The test string is in place of a Directory.CreateDirectory which is the next line (when working). Here are the results of debugging these lines:
sPath: "C:\\Users\\[user]\\AppData\\Roaming"
test: "\\Microsoft\\Windows\\Start Menu\\SkillControl\\"
I was expecting "test" to result in the full path:
C:\\Users\\[User]\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\SkillControl\\
but it seems to ignore the combine function. Can anyone work out why?
To clarify before it's asked, sPath is just a way for me to confirm if Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) was pulling the correct location which it is, I get the same results when doing
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
#"Microsoft\Windows\Start Menu\SkillControl\"));
Your second path is an absolute path - it starts with a backslash. The method is behaving as documented:
If path2 contains an absolute path, this method returns path2.
Just remove the leading backslash and it should be fine.
string sPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string test = Path.Combine(sPath, #"Microsoft\Windows\Start Menu\SkillControl\");
Related
The code below i use to read lines from file test.txt in my folder Resources of my project.
string[] test = File.ReadAllLines(#"Resources\test.txt");
The properties are already change to "Content" and "Copy Always".
When i run the program, somtimes the path auto change to the Absolute path as:
"C:\Users\Documents\Resources\test.txt
And the program error because cannot find the path.
You are using a relative path to the file, which relies on the CurrentDirectory being valid. This is either changing, or not being set to the desired directory when the program is executed. You can test this failure with this code:
string CurrentDirectory = Environment.CurrentDirectory;
Log.Trace($"CurrentDirectory = {CurrentDirectory}");
System.IO.File.ReadAllText(#"Resources\test.txt");
Environment.CurrentDirectory = #"C:\Tools";
// changing the current directory will now cause the next command to fail
System.IO.File.ReadAllText(#"Resources\test.txt");
You should not rely on the CurrentDirectory path being set correctly. Get the directory of the current running executable with something like this:
string ExePath = new System.IO.FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location)
.Directory.FullName;
string FullPath = System.IO.Path.Combine(ExePath, "Resources", "test.txt");
System.IO.File.ReadAllText(FullPath);
Yo could use
Path.Combine(AppDomain.CurrentDomain.BaseDirectory , "Resources", "test.txt"
to get the path.
But to your problem: I think, the problem is the ReadAllLines, because it wants to convert the string to an absolute path. So the problem shouldn't exist anymore, if you localize the string, or even make somenthing like:
var path = "" + "Resources\\test.txt";
var test = File.ReadAllLines(path);
I couldn't test this though because I couldn't reproduce your problem.
Consider the two paths, one in a solution and the other in IIS:
Solution Path:
C:\Users\userid\Desktop\File\Solution\RootName\ Folder1 \Folder1a\the.aspx
IIS path
C:\inetpub\wwwroot\RootName\webApplication\ Folder1 \Folder1a\the.aspx
Is there a way using Directory or Server or anything at all to get the FIRST folder name after the Parent Directory?
ie in the given above, Folder1 is the first folder after the parent folder.
I can get the opposite, which is the containing folder name from a filepath (in this example, Folder1a) and I can iterate it to get to the answer :
Path.GetFileName(Path.GetDirectoryName(file));
But the problem is, folders can be nested at any amount: (folder1a\folder1a1\folder1a1a...)
How can I get the first folder name after the Parent Directory given a file path?
as an aside,
If there is no foldername (ex. Folder1 does not exist) the answer should output "" (empty)
Use .. to go to previous directory, example:
C:\test\..\
is equivalent to:
C:\
Use Directory.GetDirectories to find sub Directories. It has an overload where you can pass search patterns as well:
string[] GetDirectories(string path, string searchPattern)
searchPattern
Type: System.String
The search string to match against the names of subdirectories in path.
This parameter can contain a combination of valid literal and
wildcard characters (see Remarks), but doesn't support regular
expressions.
Try this:
string filename = string.Format("{0}{1}", System.AppDomain.CurrentDomain.BaseDirectory, #"Folder1\Folder1a\the.aspx");
Below is what I have tried that works.
Since I simply needed to output the first page after the word, I iterated Path.GetDirectoryName and Path.GetFileName until I could match the root folder.
Ex:
baseFoldr = "";
var curFile = Path.GetDirectoryName(file);
string curName = Path.GetFileName(curFile);
while (curName.ToLower() != "RootName")
{
curFile = Path.GetDirectoryName(curFile);
baseFoldr = curName;
curName = Path.GetFileName(curFile);
}
return baseFoldr;
I still believe there could be a better answer, though
Im getting a "NotSupportedExeption was unhandled by user code - the specified path format is not supported" error, even thou I use a string as is requered.
string path = folder + "/" + filename;
fileByte = File.ReadAllBytes(path); // error here
any idea to what the problem is?
edited the code to this
string path = Path.Combine(folder, filename);
fileByte = File.ReadAllBytes(path);
the path is "F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17-04-2012\Skirmer 17-04-2012\Billeder\Galleri\F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17-04-2012\Skirmer 17-04-2012\Billeder\Galleri\2011\Vingsted\DSC_0001.JPG"
Error still happens. What I see is that ReadAllBytes requeres a string that shows the path, that I got, but it still shows error
You should not use / in the path, as the slash is an invalid character in Windows. Use Path.Combine to create it instead:
string path = Path.Combine(folder, filename);
I think that you want to use a backslash, or rather the property Path.DirectorySeparatorChar that returns the correct separator regardless of the file system:
string path = folder + Path.DirectorySeparatorChar.ToString() + filename;
Or you can use the Path.Combine method:
string path = Path.Combine(folder, filename);
What is the exact value of path variable?
Additionally, you should use Path.Combine to concatenate path parts into a full path.
As the documentation for File.ReadAllBytes states:
NotSupportedException - path is in an invalid format.
Your path is not in the correct format:
NotSupportedException path is in an invalid format.
MSDN: system.io.file.readallbytes
If the path you posted in your edited question really is the path you are trying read from then the reason you are getting the exception is because you have two colons in the path. The drive letter is repeated twice (F:\...F:\...).
The reason you end up with that path depends exactly on the contents of folder and filename in your call to Path.Combine(). It is unlikely that both folder and filename both start with the full path since Path.Combine() will return filename as the combined path in that case. It is most likely that your folder variable already contains two copies of the base path, with two drive letters and hence two colons and therefore causing the NotSupportedExeption, before you call Path.Combine().
I'm getting the error "Access to the path 'LocalApplicationData\MyProgram\' is denied." when trying to create a directory for my log file. This is when I'm running the program as a non-admin user.
Directory.CreateDirectory(System.Environment.SpecialFolder.LocalApplicationData + "\\MyProgram\\");
Why would this be?
Thanks
LocalApplicationData is just an enum value. You will have to use it in combination with GetFolderPath:
string folder = Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.LocalApplicationData),
"MyProgram");
You're trying to access the enumeration value LocalApplicationData as if it were a string. It's not. You need to find the folder path with GetFolderPath:
string path = Environment.GetFolderPath(
System.Environment.SpecialFolder.LocalApplicationData);
Incidentally, it's better form, and less error-prone, to use Path.Combine to build up paths, rather than doing it by hand:
string path = Path.Combine(#"C:\", "dir"); // gives you "C:\dir"
...and so your code would end up looking like:
string appDataPath = Environment.GetFolderPath
(System.Environment.SpecialFolder.LocalApplicationData);
string path = Path.Combine(appDataPath, "MyProgram");
Directory.CreateDirectory(path);
string targetPath = #"C:\Program Files\saadhvi\SetupSafetyPADUniversal\";
string createDatabasesScriptFilePath = Path.Combine(targetPath, "\\EADBScripts\\CreateDatabases.sql");
i am getting the value of createDatabasesScriptFilePath is \EADBScripts\CreateDatabases.sql
but i expected it would be C:\Program Files\saadhvi\SetupSafetyPADUniversal\EADBScripts\CreateDatabases.sql
what is the wrong with my code?
Here is why your code is returning the 2nd path (copied from MSDN help) -
If path2 does not include a root (for example, if path2 does not start with a separator character or a drive specification), the result is a concatenation of the two paths, with an intervening separator character. If path2 includes a root, path2 is returned.
Remove the first \ from the string "\EADBScripts\CreateDatabases.sql"
I'm not completely sure of the reason, but a I guess Path.Combine wants as second parameter a relative path, and a relative Path does not start with a \.
Remove the initial backslash from "\EADBScripts..." in the second argument.
string targetPath = #"C:\Program Files\saadhvi\SetupSafetyPADUniversal\";
string createDatabasesScriptFilePath;
createDatabasesScriptFilePath= Path.Combine(targetPath, "EADBScripts\\CreateDatabases.sql");