I'm using System.Net.FtpClient to work with FTP server in my web application.
After creating a directory and set it as working directory and uploading files in it, I want to go back one step (the parent directory) to do the same for other folders and files, but I don't know how to get the parent and set it as working directory.
I assume you use a relative path to enter the subdirectory like below.
ftpClient.SetWorkingDirectory("subdir");
Were you using an absolute path, I assume you would know how to open the parent directory.
To enter a parent directory, use "..":
ftpClient.SetWorkingDirectory("..");
See Representations of paths by operating system and shell on Wikipedia.
You can always use the absolute path:
ftpClient.SetWorkingDirectory("/path/to/parent/directory");
Related
sorry first time user and currently learning c# at uni, i'm working on an assignment and i'm trying to get the file path to work on the memory stick as that's what i need to hand it in on, thanks many regards
I'm not completely sure what your question is by I think what you're talking about is absolute vs relative paths. If you use an absolute path like "C:\users\yourname\blalba\project\stuff", then it's obviously only going to only work on your computer. However, you mostly all of the time want to use relative paths. Relative paths have the root directory of the build output files for your project; where your .exe file is built for your project. This is usually in "projectdir\bin\debug" or "projectdir\bin\release". So if you put for example a file called 'test.txt' in that directory, you can simply put the relative path "test.txt" instead of "C:\users\yourname\blalba\project\bin\debug\test.txt". If you were to put 'test.txt' in the project directory, you can use the relative path "....\test.txt". "..\" means navigating one step back.
The path to the directory containing the files you are looking for is "F:\Mod005244, 1715840". The "1715840 JH" is just the name of the drive. You access different drives via the drive letter. In this case, the drive letter "F" was assigned to your flash drive.
I would suggest making the file path configurable or request input from the user of the program as the drive letter of a flash drive will not always be "F". There are even classes (such as FileBrowserDialog) that will open a graphical file browser dialog and prompt the user to navigate and select a file.
I have an Image Path which is of D: drive and I want to display that Image on my Image Control of asp.net.So, how to provide file path to Image.ImageUrl?
"D:/Folder/001_001.jpg"
I tried with server.MapPath() method but doesn't work.
If you're deploying in IIS, you could create a virtual directory Images that would point towards D:/Folder, then you could set your ImageUrl to ~/Images/001_001.jpg.jpg.
An alternative solution could be to create a symlink between the two folders. You would run something like the following in cmd mklink /D ""D:/Folder" "[YouProjectPath]/Images", then set your ImageUrl to ~/Images/001_001.jpg.jpg. I would stay away from this though as it would only work on a machine where the project lives in the exact same path as yours.
You can make local path as following "file:///D:/Folder/001_001.jpg".
However, it's strongly not recommended to use this practice even if your site is planned to stay on your local computer only and will never been deployed to real Internet. You should copy / upload the file to your site's subdirectory 'Images' and then use the relative path '~/Images/001_001.jpg'.
I'm trying to read files from a folder which is hosted within a DFS namespace. DirectoryInfo is unable to handle it, claiming the path doesn't exist, so I find myself needing to resolve to conventional UNC paths. This I can do to a certain extent: I can take the first part i.e. \\domain\data and map it to \\fileserver1\share by calling NetDfsGetInfo() from netapi32.dll as seen in questions elsewhere on SO.
The problem is, that's not enough. Within my DFS path is another folder, which isn't a real folder, it is I believe a folder with a folder target, which points to a directory not in the root of the target share. So while the folder path in DFS is \\domain\data\documents it resolves to \\fileserver1\share\data\documents.
Is there a way I can determine this programatically? If I pass \\domain\data\documents to NetDfsGetInfo() all I get back is \\fileserver1\share, and I haven't yet found anything that lets me examine that folder and figure out where that goes.
If only DirectoryInfo could just handle all this transparently for me like Windows Explorer does.
this is a very simple issue.
Note: I am sure people have found and posted this same issue somewhere but I can't figure out the correct search terms to find it.
Okay, so here is my issue.
Let's say my program is stored at C:\Program Files (x86)\MyProgram\Program.exe.
Now in the program, it basically does
Directory.CreateDirectory(Application.StartUpPath + "\\Files")
So basically, this would create a directory called Files in the same folder as the program itself.
Assume that I have to create the folder in that location, so using a different location is not an option.
So the real problem is, if its located in the c:\Program Files directory, my program gets "access denied" when I try to create the folder.
So how can I get something like this to work without forcing the user to run it as an admin?
If it's in Windows 7, if UAC is elevate, you won't be able to modify anything in c:\Program Files without rooting yourself via 'run as administrator'.
And since windows is a multi-user operating system, storing anything user-specific there is a recipe for disaster.
The right place for your program to put its data is in the appropriate special folder, which you can get/create via either
// user-specific application data is stored here
string userSpecificAppDataDirectory = Environment.GetFolderPath(
SpecialFolder.ApplicationData ,
SpecialFolderOption.Create
) ;
// application data common to all users is stored here
string commonAppDataDirectory = Environment.GetFolderPath(
SpecialFolder.CommonApplicationData ,
SpecialFolderOption.Create
) ;
or one of the other Environment.GetFolderPath() overloads.
In modern operating system the folder C:\program files (x86) is write protected by the OS. You can't create sub folders here without using an administrative account (and also in that case you will be asked to confirm this action unless you disable UAC). So the correct way to follow is to create your data folder in another place. The best option is the CommonApplicationData folder extracted using:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
or the SpecialFolder.ApplicationData enum if your data should be differentiated by the current user of the application, or the SpecialFolder.MyDocuments if these files are produced by your user and need to be opened freely by other programs (or need to be included in a backup)
After you get the special folder provided by the OS to store application data remember to create a subfolder for your application and the other subfolders as required by your requirements
// In Win7 this usually resolves to C:\ProgramData, but do not use this folder
string appCommon = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
// This resolves to C:\programdata\your_app_name\files
string appData = Path.Combine(appCommon, "your_app_name", "files");
// This will create all directories in the specified path one by one....
if(!Directory.Exists(appData)) Directory.CreateDirectory(appData);
You could try creating any files or folders you need during your installation procedure. I am not sure what success you would find if using a third party install creator, but my understanding is that items added via the Application Files... button in the Publish tab of the Project Properties section in Visual Studio then have access privileges kind of inherently granted for use by the application.
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.