How to get user videos directory? - c#

I tried this:
userVideosDirectory = Directory.GetParent(Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData)).FullName + "\\Videos";
But that give me:
C:\Users\username\AppData\Videos
But I don't have the directory AppData there only:
C:\Users\username\Videos
How do I get the Videos directory without the AppData ?

You can get the videos folder using Environment.SpecialFolders.MyVideos property, and get the actual path to it using the Environment.GetFolderPath method.
string path = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);
Note that that enum contains many more folders you can get.

Have you tried this?
string path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);

Something like that:
int userVideosDirectory = (Directory.GetParent(Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData)).FullName + "\\Videos").IndexOf(#"\AppData\");
if (userVideosDirectory != 0)
{
string str = (Directory.GetParent(Environment.GetFolderPath
(Environment.SpecialFolder.ApplicationData)).FullName + "\\Videos").Remove(userVideosDirectory, #"\AppData".Length);
}

Related

How to get parent directory in a path c#?

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;
}

Could not find a part of the path C# asp.net when I generated user session folder in particular directory

string UserFolder = Session["Username"].ToString();
if (!Directory.Exists("~/MisReports/EmailAttachment/"+UserFolder))
{
Directory.CreateDirectory("~/MisReports/EmailAttachment/"+UserFolder);
}
filePathE = Server.MapPath("~/MisReports/EmailAttachment/" + UserFolder + "/");
filePathE = filePathE + a + ".pdf";
bool isExist = File.Exists(filePathE);
if (isExist)
{
File.Delete(filePathE);
}
report.ExportToDisk(ExportFormatType.PortableDocFormat, filePathE);
I get the error
Could not find a part of the path
if (!Directory.Exists("~/MisReports/EmailAttachment/"+UserFolder))
{
Directory.CreateDirectory("~/MisReports/EmailAttachment/"+UserFolder);
}
in this area code does not enter if check although that the folder has not been created
The ~ is probably not evaluated if you are running on Windows. Windows is not Unix. Read up on Path.Combine, Environment.GetFolderPath and Environment.SpecialFolder on MSDN. You should build a path with code like
string directoryName = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments), "MisReports/EmailAttachment", UserFolder);

C# Targeting a directory of which a mapname is partially known

I am trying to send a file using the smtp from gmail, but I have stumbled upon a problem.
The file will be stored in the windows appdata folder.
To add the file to the e-mail, I'm using:
attachment = new System.Net.Mail.Attachment((Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Folder1/Folder2/Folder3/result.txt"));
The code as above works, BUT:
The issue I currently have, is that Folder2 as seen above, will be a random name containing numbers, letters, and the word TEMP.
For example a12TEMP34b
I have tried and searched if I'm able to use * somehow, but can't seem to get it working.
Any ideas?
You can use Directory.EnumerateDirectories to search for a specific folder :
var folder1 = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Folder1");
var folder2 = Directory.EnumerateDirectories(folder1, "*TEMP*").Single();
var path = Path.Combine(folder2, "Folder3/result.txt");
attachment = new System.Net.Mail.Attachment(path)
You could parse Directory.GetDirectory into a string array and grab the first element of that array if you're sure it will always be that path.
So:
string staticPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "/Folder1/";
string dynamicFolder = Directory.GetDirectory(staticPath, "*TEMP*")[0];
string finalPath = dynamicFolder + "/Folder3/result.txt"

Get folder on in same directory as App domain path

I have an asp.net web app and i need to get the string path of a folder in the same directory as my web app.
Currently im using this code to get the add domain path.
string appPath = HttpRuntime.AppDomainAppPath;
Which returns "c:/path/webapp", i need "c:/path/folder".
Thanks.
If you'd like a more generic approach that doesn't require knowing the starting folder:
//NOTE: using System.IO;
String startPath = Path.GetDirectoryName(HttpRuntime.AppDomainAppPath);
Int32 pos = startPath.LastIndexOf(Path.DirectorySeparatorChar);
String newPath = Path.Combine(startPath.Substring(0, pos), "folder"); //replace "folder" if it's really something else, of course
This way, whatever directory your web app is running from, you can get it, reduce it by one level, and tack on "folder" to get your new sibling directory.
You can use String.Replace method.
Returns a new string in which all occurrences of a specified string in
the current instance are replaced with another specified string.
string appPath = HttpRuntime.AppDomainAppPath;
appPath = appPath.Replace("webapp", "folder");
Here is a DEMO.
Thanks to DonBoitnott comments, here is the right answer;
string appPath = #"C:\mydir\anotherdir\webapp\thirddir\webapp";
int LastIndex = appPath.LastIndexOf("webapp", StringComparison.InvariantCulture);
string RealappPath = Path.Combine(appPath.Substring(0, LastIndex), "folder");
Console.WriteLine(RealappPath);
This will print;
C:\mydir\anotherdir\webapp\thirddir\folder

Need to get application folder

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

Categories