c# FileBrowse changes path for the rest of my application - c#

i have created a form with FileBrowse control to load a file from.
the problem is, after i load a file the application looks for files in the path i'v selected instead of the 'Debug' directory (where files should be...)
how can i avoid it ? is it normal behavior ?

You can avoid it by not relying on the current directory being anything. Just consider what happens if you create a shortuct to your application, and change the startup directory.
If you want the directory where the application is why do you look for the current directory? You can get the directory of the application with the following:
Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
If this is a Windows Forms application, you can get it much easier:
Application.StartupPath

Answered Here: Why does OpenFileDialog change my working directory?
And Here: Why the current working directory changes when use the Open file dialog in Windows XP ?

This beahviour is part of Windows Common Controls (OpenFileDialog) and has nothing to do with your application configuration.
However, you can set the initial directory in your application.

Check the control for a property called RestoreDirectory. Is this set to True? If yes, try it as false.

Related

SaveFileDialog - how to get preselected directory for saving files without opening dialog

How to get directory which will would be selected on save file dialog open? I suppose it must be some value saved in Windows maybe per application because same value will be used between different application runs. Plus differs between different applications.
I would like to save file on location which was selected in previous application run without need to open SaveFileDialog. And I would like to avoid any value storing by myself.
FileDialog instances keep last directory within process execution in InitialDirectory property.
You can keep default path within runnings by using settings for example.
In the solution explorer, go to Properties section and double-click on Settings.settings: you can add here a parameter named "DefaultDirectory" for example (string).
And use it in code:
// Initialize if default path is empty (check at app startup)
Properties.Settings.Default.DefaultDirectory = "C:\\Folder";
// Set the default path (in Forms.Loaded or elsewhere)
SomeDialog.InitialDirectory = Properties.Settings.Default.DefaultDirectory;
EDIT: my answer to the question is in fact to use a value in the registry shared across the apps (or in a file in a shared system or user folder).

Does File.Exists not work inside a UWP project?

File.Exists(filePath); works perfectly inside a console application, but when I do the same thing inside uwp it doesn't detect a file.
I have tried to put breakpoints on various methods and stepped into anything that could give me some information about the issue, but I'm getting no information at all no matter what i try.
Code from UWP app:
string path = #"C:\Users\Name\Desktop\image.jpg";
if (File.Exists(path))
{
ProcessFile(path);
}
else if (Directory.Exists(path))
{
ProcessDirectory(path);
}
UWP:
When it hits File.Exists i get a return value of false, and yes i know for a fact the image is where it is.
Console:
When it hits File.Exists i get a return value of true, then goes onto the called method without any issues.
I'm expecting my code to find a File and pass the filePath into my method called "processFile".
Does UWP applications not have access to files outside of its LocalStorage or is it another issue that I'm not seeing?
UWP does not have direct access to files outside of the application folder and application data folder. That are the only two locations accessible via the System.IO APIs.
You can use StorageFile APIs to access more locations if you enable appropriate capabilities - like access to libraries or broadFileSystemAccess or use file/folder pickers. In particular, broadFileSystemAccess allows you to access the whole file system, but your app should have a good reason to do so (otherwise it will not pass the Microsoft Store certification process).
For more info see the Docs.
This is correct. UWP apps are sandboxed and cannot access files outside of LocalStorage in this way.
If you want to open a file on the users file system you have to use FileOpenPicker.PickSingleFileAsync or similar to prompt the user to pick a file, which you'll then be able to work with.
Further reading: Working with Files in UWP applications

How to write a file in WPF for Windows Screen Saver

I developed a custom screen saver in WPF (with the extension .scr) but I got an issue :
When screen saver is run by Windows, the application stop imediately.
After some tests, I found that it comes from the fact that I'm creating/writing files.
I tried :
to add a manifest to run as administrator
name my file with 8 characters + 3 for extension
write my file in different location ("C:\", "C:\Windows", "C:\Program Files (x86)",...)
But nothing works...
Any idea ?
Note : I need to write lot of lines in my file
Ok, my bad... I found where were my problemss.
First, screen saver does not have permission to write in Windows folder.
Then, I was trying to write in a special folder using Environment.SpecialFolder.MyDocuments but this property return the name of the folder. So I need to use Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) instead.
Finally, I was trying to write a file in a folder that didn't exists. I thought that the folder would be created automatically but I was wrong...
As suggested by #elgonzo, I have added exception handler and displayed the error messages in a message box using MessageBox.Show(log).

Couldn't write file to Application.StartupPath in Windows 7

I created a winform application and then created a setup of that application. this application records some info at Application.StartupPath in a file. unfortunately i got exception when i try to write the file 'Access to is denied'. Please guide me how can i get rid of that..
Thanks
You should never assume that the application startup path is writable by anyone besides system administrators, especially on modern Windows systems.
Instead of storing your file there, I'd suggest you use the folder returned by Environment.GetFolderPath(SpecialFolder.ApplicationData). That folder is guaranteed to be writable by the current user.
You can find the Microsoft guidelines about this issue here.
That's typical - you shouldn't be writing to the "Program" area of your application. You should be writing to a data area of the file system - perhaps the user's settings area, or a common application settings area.
Basically the policy was toughened up (in Vista, I believe) to try to discourage programs from doing exactly what you're currently doing. The best approach isn't to work round it - it's to change where your application stores its settings.

How to have a program know where it has been launched?

Imagine I have a picture viewer application made with C# and .NET. I have already set the preferred application to view pictures to use the C# application.
I want to somehow let my program know where it has been invoked. How can I achieve this?
If you're using it to view pictures via shell associations, you can just check the picture filenames passed in on the command line. You can use Environment.GetCommandLineArgs to get the first filename:
// Should check to make sure there is at least one filename passed first...
string imageFilename = Environment.GetCommandLineArgs[1];
string directory = System.IO.Path.GetDirectoryName(imageFilename);
If you want the working directory, just check Environment.CurrentDirectory at startup...
I think you can use Environment.CurrentDirectory
The current directory (Environment.CurrentDirectory) of an application can change during execution. Additionally, the current directory may not be the directory in which the application resides, such as if a user runs it from a command line in an arbitrary directory by specifying an absolute path to the executable.
If you really want the "current directory" of the application, then use Environment.CurrentDirectory, but if you want to know the location of the application, you can use the following approaches:
System.Windows.Forms.Application.ExecutablePath
(if running a WinForm application)
System.Windows.Forms.Application.StartupPath
(if running a WinForm application)
System.Reflection.Assembly.GetEntryAssembly().Location

Categories

Resources