Process.Start 'The system cannot find the file specified' [duplicate] - c#

This question already has an answer here:
C# relative path not start from working directory
(1 answer)
Closed 3 years ago.
System.Diagnostics.Process.Start("../Shortcuts/slot1.lnk");
This line doesn't work.
I have checked that the file ending is correct and that the file is in the correct directory and yet it still doesn't work.
It should start the shortcut but it just says that it doesn't find the shortcut.
the folder structure is as follows /folder1/Shortcuts/slot1.lnk and the program is /folder1/program/program.exe
I would like to mention that I also load pictures the same way... They work this doesn't
pictureBox1.BackgroundImage = Image.FromFile("../Slots/slot2.jpg");
folder structure for this is the same so /folder1/Slots/slot2.jpg
and the program is /folder1/program/program.exe
I got it working by using double slashes
System.Diagnostics.Process.Start("..\\Shortcuts\\slot1.lnk");

This worked for me:
System.Diagnostics.Process.Start("C:\\Users\\maple\\Desktop\\slot1.lnk");
Note the absolute path, and the backslashes instead of forward slashes.
Once you get it working with absolute path, you can try the relative path, but be aware that generally you don't control what the current directory is when your user starts your application. If you really want to use relative paths, you should explicitly set current directory in your code.

Related

Creating DirectoryInfo object for drive root [duplicate]

This question already has answers here:
c# why when the path is "C:" the directoryInfo takes me to the application folder?
(4 answers)
Closed 5 years ago.
In a C# program I am creating an instance of DirectoryInfo. Normally it does not seem to require a trailing slash after a directory name. But if I pass in "C:", rather than getting the root directory for my hard drive I get the directory where my executable is! This certainly seems like a bug but is there some hidden behavior that I am missing?
It isn't explicitly called out in the documentation, but using just (drive): isn't listed as a valid path specification among those that are listed.
The behavior you are seeing is as implemented though, as you can see from the .NET sources:
http://referencesource.microsoft.com/#mscorlib/system/io/directoryinfo.cs,90
The Init method (called from the constructor) does a check for this case, and if it finds it, uses the current working directory (".") instead. Depending on how you launched the EXE, the current working directory could be the location of the EXE.

How do i find file paths in c# windows application? [duplicate]

This question already has answers here:
Loading image from code using relative path in Windows Forms
(2 answers)
Closed 5 years ago.
So, in my program i have a folder called resources, this stores images which i load in the program. Currently i have the path hard-coded in but my friends now want to use the program, is there any way to get the file path to this folder no matter which computer it's on? surely there must be a way.
The current file path is:
H:\Desktop\Solutions\Home\PokeSheet\PokeSheet\Resources
I want it to be able to find the H:\Desktop\Solutions\Home part on its own as that is the part that will change each time
To do this you can use Directory class:
Directory.GetCurrentDirectory();
Or:
// As suggested by Martin Bäckström
AppDomain.CurrentDomain.BaseDirectory;
These will return the directory where program is executed. You need only to concat Resources string to obtained path.
Directory.GetCurrentDirectory(); changed if you open a FileDialog. To solve this you need to do this:
yourDialog.RestoreDirectory = true;
Anyway, to use Resources the best way is:
Resource.YourResource;
Where Resource is the name of you resource file/class.
You can find application startup path in StartupPath static variable:
System.Windows.Forms.Application.StartupPath;

What causes Directory.GetFiles() to append the given path to the path the program is run from?

I'm using Directory.GetFiles() to get all the files in a path on a network location.
However I'm running into an issue where it is appending the path I give it to the path that the program is running from.
So,
string workingDir = "\\1.1.1.1\c\path\to\directory\"
string dirToSearch "\\1.1.1.1\path\to\search\"
But when I run the program Directory.GetFiles(dirToSearch); it searches for files in
\\1.1.1.1\c\path\to\directory\1.1.1.1\path\to\search\
I've found no cause for this in the documentation or in my searches on Google and I'm not even really sure where to start debugging this.
MSDN:
The path parameter is permitted to specify relative or absolute path
information. Relative path information is interpreted as relative to
the current working directory
Put double slashes like for each \ put extra slash so it will be clear that this is absolute network path
Or even simpler, like said in comments below:
string dirToSearch #"\\1.1.1.1\path\to\search\"

How to get Application data folder? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can i get the path of the current user’s “Application Data” folder?
Windows XP Application Data Folder?
I have to save some settings in application data but, when i use something as "#C:\Documents ..." someone can run windous on D:\ So how to get that directory ??
You can use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); ...
And there's exaple, how you can use it:
string path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
and it returns something like C:\\Users\\UserName\\ApplicationData
and you can use Environment.SpecialFolder.Desktop too so you can get to desktop of actual user...
see the code at the end of this:
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx
Look at this MSDN entry to get the application data directory, Environment.SpecialFolder.
What I used to do is use Evironment.SystemDirectory and then break that down depending on what I need. But if you are worried about the drives then use the DriveInfo class by doing DriveInfo.GetDrives()

full path of a folder in c#

i need to get the full folder path in a windows project using c#.I tried with path.getFulPath(filename).bt it returns the application path+filename.how can i get the actual path like "D:\eclipse_files\ads_data"?
A relative path such as myfile.txt is always resolved in relation to the current working directory.
In your case the current working directory seems to be D:\eclipse_files\ads_data so your relative file path gets resolved to D:\eclipse_files\ads_data\myfile.txt when you call Path.GetFullPath.
To solve the problem, either make sure that you start with an absolute path from the beginning, or, that your working directory is set correctly.
You can get/set the working directory using the Directory.GetCurrentDirectory and Directory.SetCurrentDirectory methods.
Your question is not very clear, but I think you're looking for this:
string path = Path.GetDirectoryName(filename);
If I have understood correctly, you have a filename, for example 'doc.txt', and you want to have a method to return the full path of this file regardless of where the application runs from?
If this is what you ask it is not possible. Have you considered that there might be several files called 'doc.txt' on your harddrives?
The best you can hope to do it to search all harddrives, and return a list of all files found with the same name, but that will just be ridicously slow.

Categories