Creating DirectoryInfo object for drive root [duplicate] - c#

This question already has answers here:
c# why when the path is "C:" the directoryInfo takes me to the application folder?
(4 answers)
Closed 5 years ago.
In a C# program I am creating an instance of DirectoryInfo. Normally it does not seem to require a trailing slash after a directory name. But if I pass in "C:", rather than getting the root directory for my hard drive I get the directory where my executable is! This certainly seems like a bug but is there some hidden behavior that I am missing?

It isn't explicitly called out in the documentation, but using just (drive): isn't listed as a valid path specification among those that are listed.
The behavior you are seeing is as implemented though, as you can see from the .NET sources:
http://referencesource.microsoft.com/#mscorlib/system/io/directoryinfo.cs,90
The Init method (called from the constructor) does a check for this case, and if it finds it, uses the current working directory (".") instead. Depending on how you launched the EXE, the current working directory could be the location of the EXE.

Related

Process.Start 'The system cannot find the file specified' [duplicate]

This question already has an answer here:
C# relative path not start from working directory
(1 answer)
Closed 3 years ago.
System.Diagnostics.Process.Start("../Shortcuts/slot1.lnk");
This line doesn't work.
I have checked that the file ending is correct and that the file is in the correct directory and yet it still doesn't work.
It should start the shortcut but it just says that it doesn't find the shortcut.
the folder structure is as follows /folder1/Shortcuts/slot1.lnk and the program is /folder1/program/program.exe
I would like to mention that I also load pictures the same way... They work this doesn't
pictureBox1.BackgroundImage = Image.FromFile("../Slots/slot2.jpg");
folder structure for this is the same so /folder1/Slots/slot2.jpg
and the program is /folder1/program/program.exe
I got it working by using double slashes
System.Diagnostics.Process.Start("..\\Shortcuts\\slot1.lnk");
This worked for me:
System.Diagnostics.Process.Start("C:\\Users\\maple\\Desktop\\slot1.lnk");
Note the absolute path, and the backslashes instead of forward slashes.
Once you get it working with absolute path, you can try the relative path, but be aware that generally you don't control what the current directory is when your user starts your application. If you really want to use relative paths, you should explicitly set current directory in your code.

How do i find file paths in c# windows application? [duplicate]

This question already has answers here:
Loading image from code using relative path in Windows Forms
(2 answers)
Closed 5 years ago.
So, in my program i have a folder called resources, this stores images which i load in the program. Currently i have the path hard-coded in but my friends now want to use the program, is there any way to get the file path to this folder no matter which computer it's on? surely there must be a way.
The current file path is:
H:\Desktop\Solutions\Home\PokeSheet\PokeSheet\Resources
I want it to be able to find the H:\Desktop\Solutions\Home part on its own as that is the part that will change each time
To do this you can use Directory class:
Directory.GetCurrentDirectory();
Or:
// As suggested by Martin Bäckström
AppDomain.CurrentDomain.BaseDirectory;
These will return the directory where program is executed. You need only to concat Resources string to obtained path.
Directory.GetCurrentDirectory(); changed if you open a FileDialog. To solve this you need to do this:
yourDialog.RestoreDirectory = true;
Anyway, to use Resources the best way is:
Resource.YourResource;
Where Resource is the name of you resource file/class.
You can find application startup path in StartupPath static variable:
System.Windows.Forms.Application.StartupPath;

C# (WPF) - Getting Library folder paths in App.config [duplicate]

This question already has answers here:
Log4net output to My Documents
(3 answers)
Closed 7 years ago.
I'm looking to place some logfiles in the user's documents folder. The log path for my log4net logger is configured in the App.config XML, so I can't programmatically determine the path using the usual methods. Can the My Documents library be accessed by an environment variable, like pre-Windows 7 documents folders?
Is this what you are looking for?
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Edit
If you logging other than log4net. You need not store the log file path in the app.config in that case. because whatever you write in app.config you will end up overriding it. So it will be misleading and useless. If you want to pickup the MyDocuments location my suggestion is to fetch it in the code and wire it up with your logging module.
If you are using log4netuse the suggestion by #derpirscher - Log4net output to My Documents
Have a look at Environment.GetFolderPath (MSDN)
Now, as you clarified it's for log4net have a look at the following question
Log4net output to My Documents

how to find path to folder in c#? [duplicate]

This question already has answers here:
Relative path to absolute path in C#?
(8 answers)
Closed 8 years ago.
My main function takes arguments from command line.
This arguments looks like
--args ../../TempImages ../Resources.csproj etc.
How can i find full path to this folder in c# from this arguments?
Like
Users/%username%/Projects/Resource/Resource.csproj
I tried use Path, Directory, Enviroment classes, but nothing helps me
I am using Xamarin, MacOSX 10.9
EDIT:
Forget to say, that i don't know fullpaths as provided above. My script are running on different systems. So, fullpath to these files can be different.
Moriarty's answer helps me, but with this condition, it doesn't work.
Luckily Mono has adopted this quite well in the System.IO space. You're in the right direction with the Environment classes, like so:
var path = Path.Combine (
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Projects",
"Resource",
"Resource.csproj");
The Environment.GetFolderPath, in combination with Environment.SpecialFolder.UserProfile, will do the trick to fetch what is known in Windows as %USERPROFILE%.
Second, notice I've split the /Projects/Resource/ part in seperate arguments for the Path.Combine function. This is to prevent issues with Windows<>Mac<>Linux file operations and let mono figure this out for you.
So this'll work on all platforms. Enjoy!
Update:
For the downvoters that consider this to be a wrong answer: the suggestion mentioned in the referenced duplicate article does not work on Mac or Linux. This is because "/paths/with/slahes" act differently on Mono/Xamarin platform in non-Windows environments. And this is what the original question was about. #imho

Creating a custom file property in C# [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Can one add custom properties to NTFS folders?
I am trying to write a standalone executable in C# that attaches a value to a text file (without changing the content of the file) which can be read later.
I have looked into the following solutions but none of them quite work:
Shell32.dll: Only appears to allow reading the properties. I could not find any information on how to use Shell32 to write properties.
DSOFile: App needs to be standalone and I am also concerned about future OS compatibility.
NTFS Alternate Data Streams (ADS): Many tools will not copy this information if the file is moved or copied and I am concerned about future compatibility.
Is there another way (COM, Etc..) to attach a custom property to a text file in .NET?
The only three ways I can think of:
ADS - however you've already dismissed this.
Add it to the file name.
Add it to the beginning or end of the file.
If it's an application controlled file that may be moved around then your best bet is number two. Of course, this means that the file may "lose" that info in a renaming operation.
The third option is a bit iffy. If you can control how the file is viewed, then you can simply give it a different extension and write your data to the top of it. When it needs to be opened you can "extract" the real file and open accordingly. I think this is WAY more complicated... but it will contain your data and be less likely to be jacked with.

Categories