I have a list of files in WP7 isolated storage which has space character in the file name e.g. "My File Name 1.dat". I want to get all these files name in a string[]. I have used the following code but I am not getting the file names:
string searchPattern = "FolderName" + "\\*";
string[] fileList = store.GetFileNames(searchPattern);
Please help!
Take the quotes off of your FolderName variable to make it:
string searchPattern = FolderName + "\*";
Related
Beginner coder here.
I need to add the GetLastWriteTime string to my filename using the (rename) file.move method. How do I add a string using file.move?
I've looked up some similar info, and I've gotten part of the answer I need. System.IO.File.Move("oldfilename", "newfilename"); is the code I'll need help with. I tried adding a string to the newfilename, but it only supports directory.
string[] files = Directory.GetFiles("C:/foto's", "*", SearchOption.TopDirectoryOnly);
string filename = Path.GetFileName(photo);
DateTime fileCreatedDate = System.IO.File.GetLastWriteTime(filename);
System.IO.File.Move(#"C:\foto's", #"C:\foto's" + fileCreatedDate);
Expected error, string cannot be accepted in a directory place.
I've always preferred to use FileInfo objects for stuff like this as they have the dates built in, have MoveTo rather than using the static File.Move etc ...
FileInfo[] fis = new DirectoryInfo(#"C:\foto's").GetFiles("*", SearchOption.TopDirectoryOnly);
foreach(FileInfo fi in fis){
//format a string representing the last write time, that is safe for filenames
string datePart = fi.LastWriteTimeUtc.ToString("_yyyy-MM-dd HH;mm;ss"); //use ; for time because : is not allowed in path
//break the name into parts based on the .
string[] nameParts = fi.Name.Split('.');
//add the date to the last-but-one part of the name
if(nameParts.Length == 1) //there is no extension on the file
nameParts[0] += datePart;
else
nameParts[nameParts.Length-1] += datePart;
//join the name back together
string newName = string.Join(".", nameParts);
//move the file to the same directory but with a new name. Use Path.Combine to join directory and new file name into a full path
fi.MoveTo(Path.Combine(fi.DirectoryName, newName));
}
Directory.Move(#"c:\foto's", #"c:\photos"); //fix two typos in your directory name ;)
I need to get the filenames of files in a directory where the filename is like a passed in string(Partnumber in my example) and put them in an array.
For example, if my passed in value = 1234_ I want to search the directory and put into the array files such as: 1234.jpg, 1234_1.jpg, 1234_2.jpg and so on.
My first attempt is my code below, which does not work for what I need to accomplish. What do I need to do here?
String Path = Server.MapPath("~/PartImages/" + Partnumber + "_" );
String[] FileNames = Directory.GetFiles(Path);
I have a dropdown with list of file names. When a file name is selected in the dropdown I do the following
string filename = ddl.SelectedItem.Text;
string path = "F:\\WorkingCopy\\files\\" + filename +".docx";
DownloadFile(path,filename);
In the file folder files may contain any extension . Since i have hard coded ".docx" in string path everything works fine. But I need to get the extension of the file name with the ddl.SelectedItem.Text alone. Can you tell me how to do this?
Things I have
1.) File name without extension in
string filename = ddl.SelectedItem.Text;
2.) Path where the file is located
string path = "F:\\WorkingCopy\\files\\" + filename
I am trying to get the file extension with these . Can any one suggest on this?
You can use Directory.EnumerateFiles() like this:
string path = "F:\\WorkingCopy\\files\\";
string filename = ddl.SelectedItem.Text;
string existingFile = Directory.EnumerateFiles(path, filename + ".*").FirstOrDefault();
if (!string.IsNullOrEmpty(existingFile))
Console.WriteLine("Extension is: " + Path.GetExtension(existingFile));
Directory.EnumerateFiles searches the path for files like filename.*. Path.GetExtension() returns the extension of the found file.
In general, I prefer to use EnumerateFiles() instead of GetFiles because it returns an IEnumerable<string> instead string[]. This suggests that it only returns the matching files as needed instead searching all matching files at once. (This doesn't really matter in your case, just a general note).
Use the Directory.GetFiles() method. Something like this
string[] files = Directory.GetFiles("F:\\WorkingCopy\\files\\", filename+".*");
This should get you an array of filenames with the same filename but different extensions. If you have only one, then you can always use the first one.
You can use Directory.GetFiles Method:
string result = Directory.GetFiles(path, filename + ".*").FirstOrDefault();
see here
here " * " is the WildCard and will search for the Filename starts with YourFileName.
you can achieve that with followed by line
try
{
var extensions = new List<string>();
var files = Directory.GetFiles("F:\\WorkingCopy\\files\\", filename + ".*", System.IO.SearchOption.TopDirectoryOnly);
foreach (var tmpfile in files)
extensions.Add(Path.GetExtension(tmpfile));
}
catch (Exception ex)
{
throw ex;
}
will this help you?
You can simply split them by dot, For example, try this code
string folder = #"F:\\WorkingCopy\\files\\";
var files = System.IO.Directory.GetFiles(folder, filename + ".*");
if (files.Any())
{
string ext = System.IO.Path.GetExtension(files.First()).Substring(1);
}
This code gives me result that the extension for this is txt file.
I'm new to C# and struggle with string parsing. I have a string like this:
C:\User\Max\Pictures\
And I got multiple file paths:
C:\User\Max\Pictures\car.jpg
C:\User\Max\Pictures\trains\train.jpg
How can I strip the base path from those file paths to get:
car.jpg
trains\train.jpg
Something like this failed:
string path = "C:\\User\\Max\\Pictures\\";
string file = "C:\\User\\Max\\Pictures\\trains\\train.jpg";
string newfile = file.Substring(file.IndexOf(path));
You want to get the substring of file after the length of path:
string newfile = file.Substring(path.Length);
Note that it's a good idea to use Path methods like Path.GetFileName() when dealing with file paths (though it's not good applyable to the "train" example).
The other answer would be to replace your path with an empty string :
string filePath = file.Replace(path, "");
There are special classes to handle filepaths
var filePath = new FileInfo("dd");
In filePath.Name is the filename of the file whitout directory
So for your scenario you want to strip base dir. So you can do this
var filePath = new FileInfo(#"c:\temp\train\test.xml");
var dir = filePath.FullName.Replace(#"c:\temp", String.Empty);
I am passing a query string parameter containing file name.
default.aspx?file=Fame+ adlabs.xml (Fame+ adlabs.xml is the actual file name on server). The file name has "+" & also blank spaces.
When I check for file name from query string as follows:
var fileName = Request.QueryString["file"];
The variable filename does not have a "+" in it. It reads as "Fame adlabs.xml" & hence I get a file not found exception. I cannot rename the xml files. Can someone please guide me into right direction.
Thanks
If you are trying to do it at the server in C#:
String FileName = "default.aspx?";
String FullURL = FileName + HttpUtility.UrlEncode("Fame + adlabs.xml");
String Decoded = HttpUtility.UrlDecode(FullURL);
You should URL encode into your javascript before sending it :
var name = "Fame+ adlabs.xml";
var url = "default.aspx?file=" + encodeURIComponent(name);
Pay attention that following char won't work : ~!*()'