dialog is an OpenFileDialog class object, and I am using ShowDialog() method.
When I use path containing relative path, like:
dialog.InitialDirectory = "..\\abcd";
dialog.InitialDirectory = Directory.GetCurrentDirectory() + "..\\abcd";
ShowDialog() crashes; what I only can do is giving a definite path, starting with a disk drive:
dialog.InitialDirectory = "C:\\ABC\\DEF\\abcd";
In this case I want the path to be 1 level up of my .exe's current directory, and then downward to directory abcd.
The .exe's current path can be found by Directory.GetCurrentDirectory(), which is perfectly fine, but I cant go on with "..")
The directory hierarchy is like:
ABC
DEF
abcd (where I want to go)
defg (where .exe is at)
So, is there any method to use "..\\" with InitialDirectory?
Or I must use definite path with it?
Thanks!
I found my own answer!!
string CombinedPath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "..\\abcd");
dialog.InitialDirectory = System.IO.Path.GetFullPath(CombinedPath);
See if the following gets you the path you're looking for:
dialog.InitialDirectory
= Path.Combine(Path.GetDirectoryName(Directory.GetCurrentDirectory()), "abcd");
The call to Path.GetDirectoryName strips off the last portion of the path, after the last directory separator, whether it's a file name or folder name.
Another way would be
openFileDialog.InitialDirectory = Path.Combine(Application.StartupPath,#"..\YourSubDirectoryName");
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.
I have create a directory name 'DbInventory' in E drive of my machine
Now in my c# application I want to get the full path of this directoty(DbInventory).
bellow I am mentioning the code I have used
DirectoryInfo info = new DirectoryInfo("DbInventory");
string currentDirectoryName = info.FullName;
But this currentDirectoryName string return the file location in C drive.
First of all, DirectoryInfo constructor takes parameter as a string with full path. When you just write a folder name, it gets the location of your Visual Studio's default path which is most common in your C:/ drive and under Bin/Debug folder.
So in my computer, your currentDirectoryName will be;
C:\Users\N53458\Documents\Visual Studio 2008\Projects\1\1\bin\Debug\DbInventory
If you want to get full path name, you can use Path.GetDirectoryName method;
Returns the directory information for the specified path string.
You are creating with the
new DirectoryInfo("DbInventory");
a new directory on the default drive (C). Take a look at:
MSDN
If you want to get the directory info object of the already created one on drive E you have to specify the path.
You can use Path.GetDirectoryName
DirectoryInfo info = new DirectoryInfo("DbInventory");
string currentDirectoryName = info.FullName;
string directoryPath = Path.GetDirectoryName(path);
try server.mappath
string currentDirectoryName =Server.MapPath(info.FullName);
hope this will work for you
There is a text file that I have created in my project root folder. Now, I am trying to use Process.Start() method to externally launch that text file.
The problem I have got here is that the file path is incorrect and Process.Start() can't find this text file. My code is as follows:
Process.Start("Textfile.txt");
So how should I correctly reference to that text file? Can I use the relative path instead of the absolute path? Thanks.
Edit:
If I change above code to this, would it work?
string path = Assembly.GetExecutingAssembly().Location;
Process.Start(path + "/ReadMe.txt");
Windows needs to know where to find the file, so you need somehow specify that:
Either using absolute path:
Process.Start("C:\\1.txt");
Or set current directory:
Environment.CurrentDirectory = "C:\\";
Process.Start("1.txt");
Normally CurrentDirectory is set to the location of the executable.
[Edit]
If the file is in the same directory where executable is you can use the code like this:
var directory = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
var file = Path.Combine(directory, "1.txt");
Process.Start(file);
The way you are doing this is fine. This will find the text file that is in the same directory as your exe and it will open it with the default application (probably notepad.exe). Here are more examples of how to do this:
http://www.dotnetperls.com/process-start
However, if you want to put a path in, you have to use the full path. You can build the full path while only caring about the relative path using the method listed in this post:
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c
It would look something like this:
string pathPrefix;
if(System.Diagnostics.Debugger.IsAttached())
{
pathPrefix = System.IO.Path.GetFullPath(Application.StartupPath + "\..\..\resources\");
}
else
{
pathPrefix = Application.StartupPath + "\resources\";
}
Process.Start(pathPrefix + "Textfile.txt");
This is for opening a file in a folder you add to your project called resources. If you want it in your project root, just drop off the resources folder in the above two strings and you will be good to go.
You'll need to know the current directory if you want to use a relative path.
System.Envrionment.CurrentDirectory
You could append that to your path with Path
System.IO.Path.Combine(System.Envrionment.CurrentDirectory, "Textfile.txt")
Try using Application.StartupPath path as default path may point to current directory.
This scenario has been explained on following links..
Environment.CurrentDirectory in C#.NET
http://start-coding.blogspot.com/2008/12/applicationstartuppath.html
On a windows box:
Start notepad with the file's location immediately following it. WIN
process.start("notepad C:\Full\Directory\To\File\FileName.txt");
I'm opening a SaveFileDialog with an initial directory based on a user-defined path. I want make sure this path is valid before passing it in and opening the dialog. Right now I've got this:
Microsoft.Win32.SaveFileDialog dialog = new Microsoft.Win32.SaveFileDialog();
if (!string.IsNullOrEmpty(initialDirectory) && Directory.Exists(initialDirectory))
{
dialog.InitialDirectory = initialDirectory;
}
bool? result = dialog.ShowDialog();
However, it seems \ is slipping by and causing a crash when I call ShowDialog. Are there other values that could cause crashes? What rules does the InitialDirectory property need to follow?
The quick and easy way to fix it would be to get the full path:
dialog.InitialDirectory = Path.GetFullPath(initialDirectory);
This will expand relative paths to the absolute ones that the SaveFileDialog expects. This will expand just about anything that resembles a path into a full, rooted path. This includes things like "/" (turns into the root of whatever drive the current folder is set to) and "" (turns into the current folder).
I have a full path as given below.
C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd
How can the DTDs "part" to be fetched from this whole part?
Desired output:
C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs
Can I use String's methods for this?
If yes, then how to fetch it?
Use System.IO.Path.GetDirectoryName() for the entire path, or new DirectoryInfo(path).Parent.Name for just the name of that one folder.
There is no directory named "DTDs" in the path you posted. IT looks like there's a file named "DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd", but the periods (.) in that path are not valid directory separator characters. Did you mean "DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd"?
If that's the case, given that entire new path, you want something like this to return a list of files in the DTDs folder:
string path = #"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk\DTDs\xhtml-math-svg-flat.dtd";
string[] files = new DirectoryInfo(path).Parent.GetFiles();
in properties window i choose Build Type as Embedded resource.
And now we finally get to it. When you choose "Embedded Resource", the item is bundled into your executable program file. There is no direct path anymore. Instead, set your Build Type to "Content" and set "Copy to Output Directory" to "Copy Always" or "Copy if Newer".
Calling
System.IO.Path.GetFileName
with the full directory path returns the last part of the path which is a directory name. GetDirectoryName returns the whole path of parent directory which is unwanted.
If you have a file name and you just want the name of the parent directory:
var directoryFullPath = Path.GetDirectoryName(#"C:\DTDs\mydtd.dtd"); // C:\DTDs
var directoryName = Path.GetFileName(directoryFullPath); // DTDs
You can also use Directory to get the directory from the full file path:
Directory.GetParent(path).FullName
Edit: Please read the OP’s question and all of her comments carefully before downvotiong this. The OP’s title question isn’t EXACTLY what she wanted. My answer gave her what she needed to solve her problem. Which is why she voted it the answer. Yes, Joel’s answer is correct if specifically answering the title question. But after reading her comments, you’ll see that not exactly what she was looking for. Thanks.
Use this ...
string strFullPath = #"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd";
string strDirName;
int intLocation, intLength;
intLength = strFullPath.Length;
intLocation = strFullPath.IndexOf("DTDs");
strDirName = strFullPath.Substring(0, intLocation);
textBox2.Text = strDirName;
System.IO.Path.GetFileName( System.IO.Path.GetDirectoryName( fullPath ) )
That will return just the name of the folder containing the file.
For
C:\windows\system32\user32.dll
this will return
system32
I'm inferring that that's what you want.
Use:
string dirName = new DirectoryInfo(fullPath).name;
You can use:
System.IO.Path.GetDirectoryName(path);
You can use Path ...
Path.GetDirectoryName(myStr);
Use the FileInfo object...
FileInfo info = new FileInfo(#"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
string directoryName = info.Directory.FullName;
The file doesn't even have to really exist.
Don't use string manipulation directly. Instead use GetDirectoryName of the Path class:
System.IO.Path.GetDirectoryName(myPath);
Path.GetDirectory on the path you have specified returns:
"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug"
Try it yourself:
var path = Path.GetDirectoryName(#"C:\Users\Ronny\Desktop\Sources\Danny\kawas\trunk\csharp\ImportME\XukMe\bin\Debug\DannyGoXuk.DTDs.xhtml-math-svg-flat.dtd");
Your question is a little strange though- there is no directory called DTDs.