How to locate the path of other drive's folder in server?
For ex:
The application is there in 'C:\SomeFolder\', now I want to export a file to the drive 'D:\AnotherFolder\' on the server. How can I achieve this?
First of all you need to have permissions to access the folder. The user that needs to be able to access the folder is the same user as is running IIS. Usualy it is a built in account IIS_* that is running the instance.
As long as this user has access to the folder, you can just use File Read / Write as you normally do.
Read this article about C# File Handling.
Example - Search for files on another drive
var files = Directory.GetFiles("D:\\", "*.txt");
This will give you an array of files with the extension .txt found on D:\.
Also read this: How to: Copy, Delete, and Move Files and Folders (C# Programming Guide)
If you mean to get a list of all drives known on a system you can use Environment.GetLogicalDrives.
Related
In my project my app receives an XML from another program. I want to save this XML file in the folder in my PC. But if there is another XML file that will come, the XML will be added in the folder not overwrite the existing XML. How can I achieve it?
I am using this code right now but I don't know why I got an access denied error.
System.IO.File.WriteAllText(#"C:\XML", abc);
First of all, the error that you receive is because of Windows UAC which denies users without Administrator privileges to write on the C:\ disk. Create a folder in which you have access, for example "c:\temp\" and write your files in there.
Also, you specify a file and not folder.
And if you don't want to overwrite the file, just make sure to generate a unique filename (a guid perhaps).
In my .net windows application am using a xml file .
But after installing the application by creating setup, while I am running that 'Access Denied' to that xml file message is shown.
I am using System.IO.File methods for doing file operations.
If any body knows the solution pls share.
Write access to program directory has been more and more restricted (starting with XP/Vista) since that is a secruity risk!
I would suggest to have the "base version" of that file readonly in your program directory...
Upon start of your app you check whether it is present in ApplicationData or LocalApplicationData or CommonApplicationData - (to get the real path at runtime use Environment.GetFolderPath). If it is not there then copy it there - in those locations your application has write access by default with no need for Administrator rights.
Program Files folder has limited access and can be modified by Administrator account. I would suggest you to save your xml file in *C:\Users\YourPCName\AppData\Local\YourAppFolder* .
This way you will be able to access and modify the file
I had this problem once so I used Isolated Storage and that fixed my problem. It may or may not work for you depending on the nature of your situation, but here's a quick tutorial on how to use it:
http://www.codeproject.com/KB/dotnet/IsolatedStorage.aspx
How can I get the path of a file on my computer or on the local area network.
I would to show a window that allows me to browse the file system when I click a button, and I want to select a file and get the path to the file. How can I do this?
P.S. I'm not looking to upload the file; I just want to get the path.
The web application is running on the server, and you don't have access to the client file system at all. Can you imagine how much of a vulnerability that would be? I know I don't want the sites I visit inspecting my file system...
EDIT: Question is for a WinForms application
For a WinForms application, you can use the OpenFileDialog, and extract the path with something like this:
If you're looking for the file path:
string path = OpenFileDialog1.FileName; //output = c:\folder\file.txt
If you're looking for the directory path:
string path = Path.GetDirectoryName(OpenFileDialog1.FileName); //output = c:\folder
In general, the System.IO.Path class has a lot of useful features for retrieving and manipulating path information.
To do this in VB.NET use the FolderBrowserDialog.
Due to security restrictions browsers include for user safety, you can't manipulate the client file system directly. You can only use to let them pick a file, and even then it only sends the filename, not the whole path.
The FileSystem API in HTML5 allows for file manipulation, but only within a sandbox for your site specifically, not browsing across the network or other files on the client system.
Instead, provide your users easy steps on how they should use My Computer (or whatever equivalent on other OS's) to navigate to the file and copy & paste the path into a simple text input box.
Have you taken a look at the Path class from System.IO ?
i want to create a small c# application. there is a text box and a button in form. if anyone enter a network path for a file and press the button, then the application must copy that file to a folder within that system. How can i do this.?
How to access a network path and how can i copy the file in that path to the system??
You can use the OpenFileDilog class in .net to browse through the files.
Also you can visit these links for Copy and other functionalities.
http://msdn.microsoft.com/en-us/library/cc148994.aspx
http://msdn.microsoft.com/en-us/library/system.io.file.copy(VS.71).aspx
Net work paths are accessed by there full UNC ie \Server\Share\drive\file. As long as you have credentials to access them. You can use system.io.file.copy to move the files.
I have a text file in Program Files. I cannot write it from a C# application while running in non-admin mode.
I am using this snippet
TextReader read = new StreamReader("C:\Program Files\......\link");
It is throwing an error that the access to the file is denied, however I can read it!
Thanks
Access to a file can be different for reading and writing. As a non-admin, it's normal to be able to read files in Program Files, but not be able to write them.
If the file is a setting for the current user, you should put it in a folder under the AppData folder. You can find the AppData folder's location by calling Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
If the file is a setting for all users on the computer, use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
See http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx for a list of other possible special folder locations.
Non-admin users don't have write access to files in C:\Program Files by default. If you want to write to a file that is accessible to all users, you should create it in C:\ProgramData.
In .net there is a class File
through which you can use the method
File.read(file path)
this return a string
so you can easily manage it
it also works in a non admin mode