Hi all,
I'm new to WPF in c#, and need to know how I can replace my username with something with the basic equivalent to %userprofile% so that the file will run on other computers.
I've looked at many questions like this, but can't seem to find what I'm looking for.
What I have so far..
Process.Start(#"C:\\Users\Alexander\Desktop\She's here\She's here..lnk");
This works on my computer, but I need it to work in all instances.
I've tried using environment.find and I can't seem to understand it.
I believe you are looking for this:
Environment.SpecialFolder.Desktop
The logical Desktop rather than the physical file system location.
Which combined with Environment.GetFolderPath returns:
The path to the specified system special folder
So you should use it like this:
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Process.Start(Path.Combine(desktop, "She's here", "She's here..lnk"));
You can use use Environment.SpecialFolderNames.
var userFolder = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
var filePath = Path.Combine(userFolder, #"She's here\She's here..lnk");
Environment.SpecialFolder.DesktopDirectory :
The directory used to physically store file objects on the desktop.
Related
I wrote a program need to call an external exe using
Process proc = Process.Start(filepath).
I specify the absolute path of the exe and it works fine. However, I need to use this program in different computers. Each time the exe has a different absolute path and I need to change the code for this part. I would like to know is there a way that I don't need to change the code? Thanks in advance!
You are asking the wrong question. Is not how to modify the API to work with your fixed requirements ("launch process w/o knowing the path", ignoring for a moment what huge security problem that is). The question you should ask is How can I modify my code to match the API I use?
Since starting a process works better if a full path is given (it also works if the executable name is in %PATH%, but that is a different topic), have you app figure out the correct path and then launch the process. There are countless ways to achieve this. Probably the safest option is to use an App.Setting that points to the path. At deployment the app is properly configured with the location of the required program. there are (many) more ways to do this, it will all depend on what you're actually trying to solve, more details would be needed.
If both exe-files are in the same folder, then
winforms:
var filepath = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), otherexename);
Process.Start(filepath);
wpf:
var filepath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, otherexename)
Process.Start(filepath);
In a windows service, you can do the following to get the directory of the currently running assembly, then to generate the right path to your exe:
var directory = Path.GetDirectoryName(
new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath);
var exeLocation = Path.Combine(directory,"myExe.exe");
I'm trying to use string[] files = System.IO.Directory.GetFiles("~/Pictures/"); to search a folder in the program for picture files. This will later be used to randomly select a picture to display in an image box.I get an error when it tries to find ~/Pictures because the method is looking in 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\Pictures\'. instead. Isn't the "~" going to make it look in the programs directory? How do I make it look in the programs directory if I don't know what it will be until the program is installed? Any help will be appreciated!!
The tilde path is an asp.net construct that represents the root of the currently running asp.net application. It has no meaning outside of the asp.net context -- Directory.GetFiles doesn't know how to work with it. GetFiles does know how to work with a regular filesystem path. So the question becomes: How do we translate the asp.net relative path to one GetFiles can work with. The answer is HttpContext.Current.Server.MapPath.
I'm not near my webserver right now, but something like
var serverPath = HttpContext.Current.Server.MapPath("~/Pictures/");
var files = Directory.GetFiles(serverPath);
should get you started.
I don't think you'll find the "~" has any significance.
Try Application.StartupPath
Supplying a relative directory, like "Pictures", instead of "~/Pictures/", should do the trick.
Just use System.IO.Directory.GetFiles('Pictures'). The path separator on Windows is \, not /, and directories below the current one are just referenced as relative path locations.
I am creating a standalone application that will be distributed to many users. Now each may place the executable in different places on their machines.
I wish to create a new file in the directory from where the executable was executed. So, if the user has his executable in :
C:\exefile\
The file is created there, however if the user stores the executable in:
C:\Users\%Username%\files\
the new file should be created there.
I do not wish to hard code the path in my application, but identify where the executable exists and create the file in that folder. How can I achieve this?
Never create a file into the directory where executable stays. Especially with the latest OSes available on the market, you can easily jump into the security issues, on file creation.
In order to gurantee the file creation process, so your data persistancy too, use this code:
var systemPath = System.Environment.
GetFolderPath(
Environment.SpecialFolder.CommonApplicationData
);
var complete = Path.Combine(systemPath , "files");
This will generate a path like C:\Documents and Settings\%USER NAME%\Application Data\files folder, where you guaranteed to have a permission to write.
Just use File.Create:
File.Create("fileName");
This will create file inside your executable program without specifying the full path.
Don't forget to add:
using System.IO;
You can get the full path to your new file with:
string path = Path.GetDirectoryName(Application.ExecutablePath) + "\\mynewfile.txt"
string path;
path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );
MessageBox.Show( path );
http://msdn.microsoft.com/en-us/library/aa457089.aspx
In modern operating systems, the accepted answer of:
var systemPath = System.Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData
);
var complete = Path.Combine(systemPath , "files");
will produce a user agnostic path like: "C:\ProgramData\files"
To produce a user-based path similar to: "C:\Documents and Settings\%USER NAME%\Application Data\files"
You should use SpecialFolder.ApplicationData or SpecialFolder.LocalApplicationData instead.
I like to give the user the choice. I would default the directory to something like Environment.SpecialFolder.CommonApplicationData and let them read and edit the path at will. If in a console app display the path in help and allow them to pass it via command line argument.
This saves them the hassle of hunting for the folder. If they point to a path you cannot write to then you throw the error and let them decide what to do.
As the title suggests, how can you get the current OS drive, so you could add it in a string e.g.:
MessageBox.Show(C:\ + "My Documents");
Thanks
Add a reference to System.IO:
using System.IO;
Then in your code, write:
string path = Path.GetPathRoot(Environment.SystemDirectory);
Let's try it out by showing a message box.
MessageBox.Show($"Windows is installed to Drive {path}");
When looking for a specific folder (such as My Documents), do not use a hard-coded path. Paths can change from version-to-version of Windows (C:\Documents and Settings\ vs C:\Users\) and were localized in older versions (C:\Users\user\Documents\ vs C:\Usuarios\user\Documentos\). Depending on configuration, user profiles could be on a different drive than Windows. Windows might not be installed where you expect it (it doesn't have to be in \Windows\). There's probably other cases I'm not aware of.
Instead, use the Shell API (SHGetKnownFolderPath) to get the actual path. In .NET, these values are easily obtained from Environment.GetFolderPath. If you're looking for the user's My Documents folder:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
Full list of special folders
You can use Environment.CurrentDirectory to get the current directory. Environment.SystemDirectory will give you the system folder (ie: C:\Windows\System32). Path.GetPathRoot will give you the root of the path:
var rootOfCurrentPath = Path.GetPathRoot(Environment.CurrentDirectory);
var driveWhereWindowsIsInstalled = Path.GetPathRoot(Environment.SystemDirectory);
If you don't mind a little parsing: Environment.SystemDirectory returns the current directory.
I want to determine the full path of certain folders. In my array, I just have the names of the folders, but when my application will get installed on another user's machine, my program must be able to determine the full-path of these folders.
How to get the fullpath?
You can check the method Path.GetFullPath, it could be useful to what you're trying to do.
Path.GetFullPath Method
Did you mean the My Documents folder and the rest? It's not obvious for me from your question.
The My Documents folder is:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
And the rest of the special system folders can be retrieved in a similar way.
By prefixing it with another Path. Which Path depends on your application.
string path = ...
string fullPath = System.IO.Path.Combine(path, folderNames[i]);
You could take a look at Environment.GetFolderPath(...)
Sounds to me that you mean actually the current path plus an additional path. I.e., suppose your application is installed in c:\installations and you need the relative path of resource\en-US, you want to find c:\installations\resource\en-US.
Normally I would go for getting the current path, but in Windows it is possible to start an application as if it is executing from a different path. A fool-proof way of getting the path of the current application (where it is installed) is as follows:
// gets the path of the current executing executable: your program
string path = Assembly.GetExecutingAssembly().Location;
// transform it into a real path...
FileInfo info = new FileInfo(path);
// ...to make it easier to retrieve the directory part
string currentPath = info.Directory.FullName;
Now it becomes trivial to get new paths.
string someRelativePath = #"reource\en-US";
string someFullPath = Path.Combine(currentPath, someRelativePath);
I know, it looks a bit contrived, but it is safer then using the current path.
FileInfo.FullName, if you are lucky and the constructor finds your file somehow (e.g. current working directory).