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

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

Related

How to check if a directory exists in Firebase Storage? [duplicate]

This question already has answers here:
Check if a folder exists of a certain name in Firebase storage
(2 answers)
Closed 3 years ago.
My setup
Unity : 2019.1.6f1
Firebase Version : 6.2.0
Firebase Product : Storage
Let's say there is directory "Text" & its immediate parent is the root directory.
That directory (from my tests) will only appear if there is/are any files within that directory. That directory will delete itself if there aren't any files in that directory.
Request
Thank you
Now is there a way to know if such a directory existing using Unity's firebase API?
I wish I could have provided some sample, I am unable to find any samples pointing to directory instead of a file.
This is not a DUPLICATE question as certain API calls are present/absent in other platforms within Firebase.
There is actually no such thing as a "folder" in Cloud Storage. There are just paths to objects that look like folders, and the Firebase console lets you navigate as if there are folders, but no folders actually exist. You'll notice that if you delete a file out of a "folder", then entire folder goes away from the console (because it never existed).
When you create a file, you don't have to create any of the parent "folders". Just identify the path of the file, and it will all exist when you write that file.

Creating DirectoryInfo object for drive root [duplicate]

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.

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;

How to get Application data folder? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the path of the current user’s “Application Data” folder?
Windows XP Application Data Folder?
I have to save some settings in application data but, when i use something as "#C:\Documents ..." someone can run windous on D:\ So how to get that directory ??
You can use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); ...
And there's exaple, how you can use it:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
and it returns something like C:\\Users\\UserName\\ApplicationData
and you can use Environment.SpecialFolder.Desktop too so you can get to desktop of actual user...
see the code at the end of this:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Look at this MSDN entry to get the application data directory, Environment.SpecialFolder.
What I used to do is use Evironment.SystemDirectory and then break that down depending on what I need. But if you are worried about the drives then use the DriveInfo class by doing DriveInfo.GetDrives()

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