it is my path example E:\test\img\sig.jpg
I want to get E:\test\img to create directory
i try split but it be img
so I try function Directory.CreateDirectory and the path is E:\test\img\sig.jpg\
say me a ideas?
The recommended way is to use Path.GetDirectoryName():
string file = #"E:\test\img\sig.jpg";
string path = Path.GetDirectoryName(file); // results in #"E:\test\img"
Use Path.GetDirectoryName which returns the directory information for the specified path string.
string directoryName = Path.GetDirectoryName(filePath);
The Path class contains a lot of useful methods for path handling, which are more reliable than manual string manipulation:
var directoryComponent = Path.GetDirectoryName(#"E:\test\img\sig.jpg");
// yields `E:\test\img`
For completeness, I'd like to mention Path.Combine, which does the opposite:
var dirAndFile = Path.Combine(#"E:\test\img", "sig.jpg");
// no more checking for trailing slashes, hooray!
To create the directory, you can use Directory.Create. Note that it is not necessary to check if the directory exists first.
You can try this code to find the directory name.
System.IO.FileInfo fi = new System.IO.FileInfo(#"E:\test\img\sig.jpg");
string dirname = fi.DirectoryName;
and to create the directory
Directory.CreateDirectory(dirname );
Another solution can be :
FileInfo f = new FileInfo(#"E:\test\img\sig.jpg");
if (f.Exists)
{
string dirName= f.DirectoryName;
}
Related
I am trying to construct a file path in order to read an XSLT file, like so:
string path = "../_xslt/example.xslt";
StreamReader reader = new StreamReader(path);
...where I am in a controller (/Controllers/ExampleController.cs), and the '/_xslt/' folder is at the same level as '/Controllers'
However, the error I am getting is:
(System.IO.DirectoryNotFoundException)
Could not find a part of the path 'c:\windows\system32\_xslt\example.xslt'.
Am I going about this the wrong way?
Thanks for any help!
You can use the HttpServerUtility.MapPath method to map any relative paths for you, in your controller this is easily accessible via the ControllerContext:
string path = ControllerContext.HttpContext.Server.MapPath("~/_xslt/example.xslt");
...
string TestX()
{
string path = AppDomain.CurrentDomain.BaseDirectory; // You get main rott
string dirc = ""; // just var for use
string[] pathes = Directory.GetDirectories(path); // get collection
foreach (string str in pathes)
{
if (str.Contains("NameYRDirectory")) // paste yr directory
{
dirc = str;
}
}
return dirc; // after use Method and modify as you like
}
If controller is present at directory root
String path = ControllerContext.HttpContext.Server.MapPath(#"~/_xslt/example.xslt");
Else
String path = ControllerContext.HttpContext.Server.MapPath(#"../_xslt/example.xslt");
I am using AppDomain.CurrentDomain.BaseDirectory and I want to go one step backwards but couldn't figure out how? Below is the example,
CODE :
string path = AppDomain.CurrentDomain.BaseDirectory;
RESULT :
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin\\Debug\\"
EXPECTED RESULT:
"C:\\Mainline Code\\IxExpress\\.NET Applications\\IXTextIndexBuilder\\IXTextIndexBuilder\\bin"
You can use something like the following to get the parent of a given directory:
string dirName = AppDomain.CurrentDomain.BaseDirectory; // Starting Dir
FileInfo fileInfo = new FileInfo(dirName);
DirectoryInfo parentDir = fileInfo.Directory.Parent;
string parentDirName = parentDir.FullName; // Parent of Starting Dir
Use following snippet:
string path = (new FileInfo(AppDomain.CurrentDomain.BaseDirectory)).Directory.Parent.FullName;
Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.FullName
I need to get my application directory and add a file name to that path. So I used it this way.
String kofaxTextFilePath = Path.GetDirectoryName(Assembly.GetAssembly(typeof(File)).CodeBase) + "\\KofaxBatchHistory.txt"
So it will give a path like this.
“file:\\C:\\Documents and Settings\\MyApplication\\ KofaxBatchHistory.txt”
But I need to get only
C:\\Documents and Settings\\MyApplication\\ KofaxBatchHistory.txt
With out doing any thing to this string is there any method to get it directly?
string myDir = System.Reflection.Assembly.GetExecutingAssembly().Location;
myDir = System.IO.Path.GetDirectoryName(myDir);
String kofaxTextFilePath = System.IO.Path.Combine(myDir, "KofaxBatchHistory.txt");
Try Assembly.Location.
Assembly.GetAssembly(typeof(File)).Location
Or (better yet):
typeof(File).Assembly.Location
See Environment.SpecialFolder enumeration, http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx.
You might be looking for SpecialFolder.CommonApplicationData
I have a collection of files with fully qualified paths (root/test/thing1/thing2/file.txt). I want to foreach over this collection and drop the file into the location defined in the path, however, if certain directories don't exist, I want them to great created automatically. My program has a default "drop location", such as z:/. The "drop location" starts off empty, so in my example above, the first item should automatically create the directories needed to create z:/root/test/thing1/thing2/file.txt. How can I do this?
foreach (var relativePath in files.Keys)
{
var fullPath = Path.Combine(defaultLocation, relativePath);
var directory = Path.GetDirectoryName(fullPath);
Directory.CreateDirectory(directory);
saveFile(fullPath, files[relativePath]);
}
where files is IDictionary<string, object>.
string somepath = #"z:/root/test/thing1/thing2/file.txt";
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName( ( somepath ) );
Directory.CreateDirectory("/root/...")
Creates all directories and subdirectories in the specified path
Check IO namespace (Directory, Path), I think they'll help you
using System.IO
Then check it..
string fileName =#"d:/root/test/thing1/thing2/file.txt";
string directory = Path.GetDirectoryName(fileName);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
string filename = "c:\\temp\\wibble\\wobble\\file.txt";
string dir = Path.GetDirectoryName(filename);
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
File.Create(filename);
with suitable exception handling, of course.
I've found setting the "default location" at the start of execution to be helpful and reduce a bit of redundant code (e.g., Path.Combine(defaultLocation, relativePath)).
Example:
var defaultLocation = "z:/";
Directory.SetCurrentDirectory(defaultLocation);
Directory.SetCurrentDirectory(AppContext.BaseDirectory);
Request.Path will get the current Path name with file name such as:
C:/......./Personal/Items.aspx
How can I get the only Path name such as:
C:/......./Personal
You can use Path.GetDirectoryName to get the directory part of a path.
var path = System.IO.Path.GetDirectoryName(#"C:\Personal\Items.aspx");
// path is #"C:\Personal"
This will return the virtual path:
Page.TemplateSourceDirectory
See the below answers for the physical path.
Use the GetParent() method on Directory.
DirectoryInfo parent = Directory.GetParent(requestPath);
you can get Directory Path from System.IO.FileInfo
var fInfo = new System.IO.FileInfo(path);
var result = fInfo.DirectoryName;