This is what i'm trying to do in my application:
On startup, the application searches for specific files (*.txt for example) in a specific folder (let's say c:\testfolder + all subfolders) and stores their path in a simple string[]. Some files might be located in the root-folder (c:\testfolder), some files have additional subfolders (c:\testfolder\subfolderA\subfolderB).
Now, when I click on a button, all selected files are moved to a temp-folder (like c:\testfolder\temp). When I now close and reopen the application, I want to move all files from the temp-folder to their original location. Obviously this won't work, since the original path was overwritten after restarting the app.
This might be an easy task, but could someone maybe give me hint on how I could do this?
/edit
Would it be possible to ignore a specific folder (temp-folder in this case) when searching for files? Basically moved files are ignored & the old path is still saved (in Properties.Settings. for example) from the first start.
Current code I'm using to get all files:
var files = Directory.EnumerateFiles(file_path, "*.*", SearchOption.AllDirectories).Where(s => s.EndsWith(".jpg") || s.EndsWith(".png"));
Related
I've got a program that digs inside the Local App Data folder of another program, pulls out some files, and then pushes them to Azure Blob Storage. I've already developed the rest of the program, but the intent of the application is to be as brainless as possible for the user- just a simple double click and the files have been uploaded.
At the moment the program requires manual input to find the correct folder in Local App Data. The Problem lies in that the name of the folder isn't always completely constant.
The folder's name always starts with com.company.propelics, followed by a series of randomized numbers and characters. I've already checked and there's no way to reproduce the randomization for each user. Within that folder, the folder structure is always constant- so the program would never have an issue finding the files once the original folder is found.
Is there a way to either scan the folders in Local App Data for the subfolders that will always exist, or take what is constant (com.company.propelics) and select the folder with that in the name?
Thanks for the help
IEnumerable<string> candidates = Directory
.EnumerateDirectories(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"com.company.propelics*");
Then
var folderPath = candidates.FirstOrDefault();
or something more complicated if there's disambiguation to perform...
I try to make a photo editor/viewer as the Windows 10 App. I let the user to open an image file by FileOpenPicker. The program also should be able to browse other images from the same folder as the selected file (e.g. by previous and next buttons), but there is the problem. I even can't get file names of these images.
All the methods I have tried just return null...
// File picker to let user select the image file - works OK
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".jpg");
var file = await picker.PickSingleFileAsync();
// These methods to get neighboring files do not work
// 1. using Directory.GetFiles - it returns empty string
string filePaths[] = Directory.GetFiles(Path.GetDirectoryName(file.Path));
// 2. using GetParentAsync - it returns null instead of file's parent folder
var folder = await file.GetParentAsync();
var files = await folder.GetFilesAsync();
So, how can I browse through the other files from the same folder as the already opened file?
I have found I can use a FolderPicker to get access to given folder, but that would be a very silly solution if the user need to go through two pickers (one for the image file and another for the actual file's folder).
EDIT: After I checked Removable Storage and Picture Library in App Manifest, the GetParentAsync started to work for USB drives, but when I open an image file from any local drive it still returns null. Why it does not work for local HDD drives, which are the logical storage device for large amount of photos? Storing data only to user folders on small SSD system drive or USB drive is so limiting.
Basically, If user pick a 'File' by FilePicker, you have the rights only for the 'File'. Not for parent folder. (If you have access rights for picture library and the file is located at the library, you can access the folder. But, the rights comes from library access, not from the file.)
This is a design of WinRT's file accessing rule via FileBroker.. It's hard to overcome.
My recommendation (and many of storeapp picture viewer choose this way, including my app PICT8) is, Ask user to set the folder that the user mainly used to keep the image files by using FolderPicker.
You can keep the access rights for the folder by using FAL or MRU. Instead of using FilePicker, You can show the list of files to your user.
StorageApplicationPermissions class
And...My answer for another thread may help you also: Access all files in folder from FileActivated file
You got a StorageFile from the FileOpenPicker. You can get the containing folder with the GetParentAsync method and then call GetFilesAsync to get the files inside the folder.
var folder = await file.GetParentAsync();
var files = await folder.GetFilesAsync();
var next = files.SkipWhile(x => x.Name == file.Name)
.Skip(1)
.First();
As you can see you can get the next file inside the folder be skipping ech file until you find your current. Then skip it and take the first after your file.
I think it would better to order the files before you acccess them by calling OrderBy respectivly OrderByDescending on the files result.
You are able to pass a lambda expression to select an property or value for sorting.
In my project , i have a download button in jqgrid. When I click this button the appropriate file must be downloaded, but here is my problem; all these files come from different folders, so I cannot set a folder name in mappath. I saw different code for downloading, but in every case there has to be a folder name.
I have gound one solution for this; to bind the folder name & file name in a grid ,but there is a huge amount of data and I cannot change the database.
My question is: how can I download a file without a giving folder name?
but here my problem is all these files come different folders, so i
cannot give folder name in mappath
you can't download a file without knowing the virtual or absolute path. In your case, you want to download a file, but certainly (your code)doesn't know where it resides. more accurately, your code doesn't have a clue to provide the path of the file.
You should be able to provide a mechanism to down the file(be it keeping the file path in a DB table or a mapping of user to folder), you wont get a generic solution to your issue.
I have successfully been able to build content into my winform using Winform Series 1 and Winform Series 2 but my question is, how do you get the content to be loaded back into the editor the next time you open it. The content, .xnb, file is currently being saved to a temp folder. Is there anyway to have the content be loaded back into the editor with out having to go and build each file again?
Could I just save it to the Content folder inside the bin/ folder and then look through that folder at the start up and look for .xnb files and just load them? or is there an easier way to this?
In the second WinForms sample there is a HTML readme file that describes how the application saves built content to a temporary directory and then deletes it when the program closes.
This is the important bit:
Depending on your application, you might prefer to always use the same temporary directory name, and never delete it. This will leave files lying around on your hard drive. The content build process is incremental. If your program tries to load the same content files that were already built during a previous run, you will not need to carry out any actual processing work. This can speed up loading times for programs such as level editors that are likely to want to load the same files each time they start up.
It says that deleting the temporary directory "is handled by ContentBuilder.DeleteTempDirectory, which is called by Dispose". So simply find the call to DeleteTempDirectory and remove it.
The readme file describes in more detail how the temporary directory is selected (and why). You could modify CreateTempDirectory to suit your application better. For example if your editor has "level" files, you might want to save your built content (.xnb files) in a subdirectory with the same name, next to your level, so that your game can easily open the built content.
Once your files are being kept between sessions - all you have to do is reload them. The two obvious ways are to store a list of the files that are open, and reload it next session. Or simply open everything that is in your output directory:
Here is some rough code to do the latter (assuming no subdirectories):
string folder = #"C:\TemporaryXNAFilesOrWhatever";
List<Texture2D> textures = new List<Texture2D>();
ContentManager content = new ContentManager(serviceProvider, folder);
string[] files = Directory.GetFiles(folder, "*.xnb");
foreach(string file in files)
{
string assetName = Path.GetFileNameWithoutExtension(file);
textures.Add(content.Load<Texture2D>(assetName));
}
say my application is installed in C:\programfiles\xyz\ I've some setting & other data files (*.dat files) in this directory too.
My application uses OpenFileDialog to open an image. Problem is that when ever user browses to some directory (say MyPictures) for an image. The current working directory of the application becomes that directory (MyPictures in this case).
After user inputs the image. I do some processing over it and save some values to imagedata.dat which will be located in the path where original application is installed.(C:\programfiles\xyz here )
In my code I'm just using FileStream("imagedata.dat",...,...) without any path prefix before the filename. Problem here is that application is crashing because it is searching for imagedata.dat in 'MyPictures\imagedata.dat'.
How can I avoid this?
You should be using absolute path names when saving data to files. The current working directory is controlled by the user, not by you (for example, if they launch your process from a shortcut then the working directory could've been changed before your process even starts up).
Also, you should never save anything under C:\Program Files during normal use. Doing this means your program needs to be running as an administrator, and unless you're doing administrator-y things then you should be able to run it as a regular user.
The correct thing to do in your case is to use the Environment.GetFolderPath() function to get the location of the ApplicationData folder and save your data under there. Just choose a sub-directory based on your application's name.
You could save it to GetCurrentDirectory then restore with SetCurrentDirectory. However, I agree wih codeka that using the appropriate GetFolderPath (probably ApplicationData, CommonApplicationData or LocalApplicationData) is a better solution.