I have a program where a user selects a file with an OpenFileDialog, I store that path (ofd.FileName) into a string FilePath, I need to get the name of the folder that the file is in, how do I do that?
Like if user selects file "C:\Users\Name\Documents\hi.txt", how do I get the folder path "C:\Users\Name\Documents" ?
The Path class offers numerous methods to handle File and Path strings
In your case you need to use
string fullFilePath = #"C:\Users\Name\Documents\hi.txt";
string pathOnly = Path.GetDirectoryName(fullFilePath);
Console.WriteLine(pathOnly);
Related
I want to read the folder name from a particular path into a text box using C# web application. When I used ASP:FileUpload control, I am able to get only the name of the selected file, not the full path. As I am working on it for the first time, I am not able to get the details exactly. Please, anyone help me get the folder or subfolder name from the selected path.
You can get full name of the file, then get the name of the directory:
string fileFullPath = Server.MapPath(FileUpload1.FileName);
string directoryName = System.IO.Path.GetDirectoryName(fileFullPath);
string[] dir = Directory.GetDirectories(path).Select(file => Path.GetFileName(file)).ToArray(); By using this code i am able to get all the directory names in the particular path.
http://pastebin.com/DgpMx3Sx
Currently i have this, i need to find a way to make it so that as opposed to writing out the directory of the txt files, i want it to create them in the same location as the exe and access them.
basically i want to change these lines
string location = #"C:\Users\Ryan\Desktop\chaz\log.txt";
string location2 = #"C:\Users\Ryan\Desktop\chaz\loot.txt";
to something that can be moved around your computer without fear of it not working.
If you're saving the files in the same path as the executable file then you can get the directory using:
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
Normally you wouldn't do that, mostly because the install path will be found in the Program Files folders, which require Administrative level access to be modified. You would want to store it in the Application Data folder. That way it is hidden, but commonly accessible through all the users.
You could accomplish such a feat by:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fullPath = Path.Combine(path, #"NameOfApplication");
With those first two lines you'll always have the proper path to a globally accessible location for the application.
Then when you do something you would simply combine the fullPath and the name of the file you attempt to manipulate with FileStream or StreamWriter.
If structured correctly it could be as simple as:
private static void WriteToLog(string file)
{
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string fullPath = Path.Combine(path, #"NameOfApplication");
// Validation Code should you need it.
var log = Path.Combine(fullPath, file);
using(StreamWriter writer = new StreamWriter(log))
{
// Data
}
}
You could obviously structure or make it better, this is just to provide an example. Hopefully this points you in the right direction, without more specifics then I can't be more help.
But this is how you can access data in a common area and write out to the file of your choice.
I would like to extract the selected path from Save dialog, excluding the file name.
The following code retrieves the full path + the file name with its extension.
placeToSaveDocument = Path.GetFullPath(saveFileDialog.FileName);
Please do not suggest to use Folder Browser Dialog instead, because I have a reason why not to use it.
Any ideas?
You're probably looking for Path.GetDirectoryName(saveFileDialog.FileName).
Example:
string filePath = #"C:\MyDir\MySubDir\myfile.ext";
string directoryPath = Path.GetDirectoryName(filePath);
//directoryPath = "C:\MyDir\MySubDir"
I've C# project and it has Resources folder. This folder has some of txt files. This files have various file names.
I'm taking file names from any source as string variable. For example I have fileName string variable and test.txt file in Resources folder:
string fileName = "test.txt";
When I want to access this file as like below, I can:
WpfApplication.Properties.test.txt;
But, When I want to access it by this code, I can't.
WpfApplication.Properties.fileName;
I want to use fileName string variable and access this text file.
What can I do to access it?
Thanks in advance.
Edit :
I change form of this question:
I've string variable assigned any text file name. For example; I have a.txt, b.txt, c.txt, d.txt, etc.. I'm taking this file name as string variable (fileName) via some loops. So, I took "c.txt" string. And, I can access this file by code in below:
textName = "c.txt";
fileName = "../../Resources\\" + textName;
However, when I build this project as Setup Project and install .exe file to any PC, there is no "Resources" folder in application's folder. So,
../../Resources\
is unavailable.
How can I access Resources folder from exe file's folder?
You need to add a Resource File to your project wich has the extension .resx/.aspx.resx. You will then be able to double click on this file and edit the required resources/resource strings. To do this right click on Project node in Solution Explorer > Add > New Item > Resource File. Let us assume you have added a file called ResourceStrings.resx to the Properties folder and added a resource string with key name MyResourceString, to access these strings you would do
string s = Properties.ResourceStrings.MyResourceString;
I hope this helps.
I would strongly recommend you taking a look at: http://msdn.microsoft.com/en-us/library/aa970494.aspx
If your text files have build action set as Resource you can locate them in code like:
(assuming the file name is fileName and its located in Resources folder)
Uri uri = new Uri(string.Format("Resources/{0}", fileName), UriKind.Relative);
System.Windows.Resources.StreamResourceInfo info = Application.GetResourceStream(uri);
Then you can access info.Stream to get access to your file.
I wrote a simple console tool that reads a file and then writes something out. I intend to just drag and drop files and then out pops the output in the same directory as the input file.
All of the testing works, and when I call it from command-line, everything comes out as expected. However, when I tried dragging and dropping it in explorer, no files were created.
I did a search through the system and found that they were all dumped at Documents and Settings under my user folder, and when I printed out the full path that's what it said.
Which is weird. Wouldn't Path.GetFullPath return the absolute path of the input file? Instead it looks like it just combined that user directory path to the input's filename.
EDIT: here's the code. I feel like I've made a logic error somewhere but can't seem to see it.
filename = System.IO.Path.GetFileName(args[i]);
abspath = Path.GetFullPath(filename);
dirpath = Path.GetDirectoryName(abspath);
....
Console.WriteLine(dirpath);
Path.GetFullPath should return the absolute path of the path string you pass in.
Path.GetFileName(string path) only returns the filename and extension of the file you pass in. For example, System.IO.Path.GetFileName("C:\SomeDirectory\Test.txt"); would just return "Test.txt". You'll want to use the Path.GetDirectoryName to get the path of your input file, like so:
string inputDirectory = System.IO.Path.GetDirectoryName(args[i]);
Alternately, you can use the FileInfo class to retrieve a bunch more information about your input file. For example:
// Assuming args[i] = "C:\SomeDirectory\Test.txt"
FileInfo inputFile = new FileInfo(args[i]);
string inputDirectory = inputFile.DirectoryName; // "C:\SomeDirectory"
string inputFileName = inputFile.Name; // "Test.txt"
string fullInputFile = inputFile.FullName; // "C:\SomeDirectory\Test.txt"