I Added textfile on my Application Folder where the application is also located.
I having a problem where to locate and read the content of the texfile regarding what location the users specify the application to be installed.
Example:
CASE 1: If Application Installed on C
Get the path of: C:\Textfile.txt
CASE 2:If Application Installed on Program files
Get the path of C:\Program Files\Default Company Name\Textfile.Text
Thanks in Regards.
i am not sure if this is what you wanted
string StartPath = Application.StartupPath; //or what SilverbackNet suggested
string FileName = "YourFileName.txt"
Textreader tr = new StreamReader(StartPath+ "\\" + FileName);
Console.WriteLine(tr.ReadLine());
tr.Close();
System.Windows.Forms.Application.ExecutablePath is pretty much the go-to for this particular situation, especially for loading libraries. Please look at System.Environment.GetFolderPath(SpecialFolder.*) for storing data, however, or you'll probably run afowl of stricter permission problems at some point.
Related
I have a dll file that iv'e put on my IIS website. Is it possible to check the assembly version from this file. I know how to do this if the file was stored on my local machine but its not. this is the code i tried:
string FilePath = "";
FileVersionInfo VersionInfo = FileVersionInfo.GetVersionInfo(FilePath);
string productVersion = VersionInfo.FileVersion;
The problem i'm getting is that when i enter a website directory in my FilePath variable it errors and says you can't enter a website path. Is there another way to use a website as a path and not a directory from my c drive??
If it is possible please give me a starting point and some websites to learn this from or some sample code. Would be much appreciated.
URL <> FileSpec
Thanks for telling us what you've tried. Sounds like you are trying to reference the DLL by the URL, not the file spec. A path like this:
http://MyNetworkWebServer/assy.dll
goes thru IIS and HTTP which intentionally bars actually viewing the dll for many reasons - the dll will be coded to serve up some content maybe but certainly not allow you to examine the bits.
A path like this:
\\MyNetworkServer\IisFileShare\assy.dll
goes thru the filesystem / filesharing areas of the server and will allow you to call those functions if proper permissions and security settings are in place.
I have created a .msi by using VS2008 setup project. My application frequently writes some value in a .txt file in the application directory (C:\Program Files\MyApp\MyFile.txt). After insalling it in Win7, it raises an exception "Access to the path .... is denied."
But whenever I run it as administrator, no such exception occurs. Here is my sscce
string FilePath=Application.StartupPath + #"\AppSettings\CurrentUserName.inf";
using (StreamWriter writer=new StreamWriter(FilePath,false))
{
writer.Write(txtLoginName.Text.Trim());
}
MainForm.ProcessLogIn();
this.DialogResult = DialogResult.OK;
I don't know how to solve this problem. Any suggestion?
Move your file out of Program Files directory. In Win7 is readonly for normal users.
You could move the file in the ProgramData directory.
Your installer should create a directory for your application there.
Then inside your code you could retrieve the correct full pathname using these lines of code
string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
string appFile = Path.Combine(dataPath, "MyAppDir", "MyFile.txt");
usually (on Win7) this result in a path like this
c:\programdata\MyAppDir\MyFile.txt
but using the SpecialFolder enum you are guaranteed to use a folder available in readwrite to your application not depending on the current operating system.
The only way to solve this problem is to not write to that folder. You are not allowed to write to that folder by convention, unfortunately, older versions of Windows did not hold you to this.
Instead, you can use Environment.SpecialFolder to help you find where you need to go:
// your application data for just that User running the app
var perUserAppData = Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData);
// your application data for ALL users running the app
var allUsersAppData = Environment.GetFolderPath(
Environment.SpecialFolder.CommonApplicationData);
// better!
var path = Path.Combine(perUserAppData, #"MyApp\MyFile.txt");
Basically, Windows 7 is telling you that you're going to have to stop driving on the sidewalks and use the street as was intended.
As a short-term fix, you can use ICACLS to grant write access to the file. Note: NOT the whole directory.
As a longer term fix, you should NOT write to the program directory if you are running as unprivileged users, but instead somewhere like %LOCALAPPDATA% or %APPDATA%.
I have an application in Visual Studio C# which includes saving into a text file, how can I have a .exe sent to another computer and not have an exception in saving?
I need to send a .exe file by email (Yes it's possible) and this application includes saving the state of the game. How can I send this .exe file and let the user be able to save in his computer please?
The problem is that when I send my application's executable file on another computer, I'm getting an exception in saving. Because on the other computer I don't have the text file which I'm saving the game.
I am saving here :
StreamWriter myFile = File.CreateText(Directory.GetCurrentDirectory()+"//ConnectFour.txt");
in the obj/Debug/ of the project..
Thanks for your help :)
Sending an executable should work just fine.
Make sure the other computer has the appropriate Microsoft .NET Framework installed.
Latest framework installer: MSDN
Also, make sure the path inwhich you're saving the file to exists on the remote computer. For example, if you're trying to save to the D:\ drive and it doesn't exist. You will get an exception.
Most likely current location is not writable by current user.
Using "current directory" is dangerous as you have no control over where application is launched from. It is very useful for command line utilities, but not so much for regular windowed applications. Use location that you know you can save files to - i.e. "My Documents".
var filePath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\",
ConnectFour.txt");
using(var myFile = File.CreateText(filePAth))
{
// save data here.
}
The problem when sending executables by email are the anti-virus-scanners. Some of them refuse e-mails containing executables. Others accept the mails but delete the attachment.
The solution consists in hiding the executable. This can be done by zipping the executable and sending the zip-file.
Often this is enough to solve the problem but some anti-virus-scanners are very smart and even recognize executables within the zip-attachment. The solution here is to encrypt the zip-file with a password. I often just use the password "pwd" and mention it in the e-mail text. The anti-viruses are not (yet) smart enough to understand this text.
UPDATE
Now I understand your problem. It has nothing to do with sending the executable.
An application can determine from which directory it has been started like this
string dir = System.IO.Path.GetDirectoryName(
System.Windows.Forms.Application.ExecutablePath
);
An alternative is (if you don't have a reference to WinForms):
string dir = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetEntryAssembly().Location
);
You probably do not have sufficient privileges to save the file on the remote machine. If you give us more information on the exact exception that is being thrown (type of exception, message, stack trace, etc) you will get a more accurate answer.
What I am trying to do is read data from a CSV file located within a Windows application folder named "Attachments". In a web application you can get the path of the folder using
Server.MapPath(#"Attachments/sample.csv");
What is the equivalent call from a Windows application?
Below is my code.
string[] str = File.ReadAllLines(#"Attachment/sample.csv");
// create new datatable
DataTable dt = new DataTable();
// get the column header means first line
string[] temp = str[0].Split(';');
// creates columns of gridview as per the header name
foreach (string t in temp)
{
dt.Columns.Add(t, typeof(string));
}
Is the path relative to the executable? If so you can use Application.StartupPath to determine where the program was started, and then combine that with the relative file path to get the full path:
var fullPath = Path.Combine(Application.StartupPath, #"Attachment\sample.csv");
If your app is running as a service or uses ClickOnce deployment this won't work, though.
The Windows Application Folder could be identified by this enum
Environment.SpecialFolder.ProgramFiles
MSDN refs
To get a string containing the actual path and the full file name you write
string pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
string fullFileName = Path.Combine(pathToFile, #"Attachment\sample.csv");
As noted by Cole Johnson in its comment, there is a problem if your hosting operating system is 64bit. In that case there are two application folders. One for 64bit apps and one for 32bit apps.
Using NET4, you could discover the current operating system bitness with the property
Environment.Is64BitOperatingSystem
and, if false, use a different enum
Environment.SpecialFolder.ProgramFilesX86
But after all this, I think you should change something in the architecture of your program.
In Web applications, usually you do not use folders outside the web-root.
But WinForms application has no such limitation and then you may install the CSV files in a different folder (MyDocuments comes to mind) and control the actual location on your hard disk via an option in the configuration file
REMEMBER: The Application Folder requires particular permission to write in (If you save your attachments there this is another reason to choose a different location than the Application Folder)
You can do this with So something like
Path.Combine(Application.StartupPath, #"Attachment\sample.csv");
Steve beat me to it but yeah, check intellisense for whatever folder you're looking for under :
string path = Environment.GetFolderPath(Environment.SpecialFolder.
properties
You'll find all kinds of system paths there.
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 ?