I have the Windows Form Application which works with 2 data sets(text files). How can change the path of text files from C Drive into the Documents folder with following adress: Libraries\Documents?
If I want copy them into the desktop what can be the path?
PS: I copy the data sets into Documents and change the
StreamReader fileitem = new StreamReader("c:\\dataset.txt");\
into:
StreamReader fileitem = new StreamReader("Libraries\Documents\dataset.txt");
But it doesnot work.
An idea?
You need Environment.GetFolderPath.
string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
Don't concatenate strings to create path, use Path.Combine instead. So when you need subfolder of desktop you'll use
string subFolder = Path.Combine(desktop,"MySubFolderName");
So in your case
StreamReader fileitem = new StreamReader(Path.Combine(desktop,"dataset.txt"));
string documents = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
StreamReader fileitem = new StreamReader(Path.Combine(documents, "dataset.txt");
Related
I'd like save date my app but I'm not knowing if app save for different users, Example if "andy" I can't use ("C:\Users\Game maker\AppData\Roaming") ,So How to create file in "AppData\Roaming\MaxrayStudyApp" for any user .
Computer myComputer = new Computer();
myComputer.FileSystem.WriteAllText(#"C:\Users\Game maker\AppData\Roaming\MaxrayStudy\data.txt","", false);
Almost a duplicate of this one: Environment.SpecialFolder.ApplicationData returns the wrong folder
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
That should get the folder you need then use Path.Combine() to write to a directory in that folder.
var roamingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filePath = Path.Combine(roamingDirectory, "MaxrayStudy\\data.txt");
I restart my windows and I write this
string filePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
filePath= (filePath+#"\MaxrayStudyApp\data.txt");
string path =Convert.ToString(filePath);
using (System.IO.StreamWriter file =
new System.IO.StreamWriter(filePath,false)){
file.WriteLine(" XP : 0");}
I'm trying to make a program that stores files as *.txt based documents. I want to be able to click a button and pull up a list of currently stored files
(Located in C:\ProgramData\ProgramName\Incidents)
Above is an example of what I'm trying to accomplish where 140219-000727 is the name of the file, the rest isn't need. Clicking Open or Double Clicking would "Open" that file and parse the .txt into pre-existing forms on a WinForm application that I have already created.
What is the best way to go about doing this with a minimal hit on system resources?
I think Directory.GetFiles is what you are looking for. You can use the simplest mask "*.txt" to fetch all txt files and then using Path.GetFileName cut the file name from the full path.
And later (on double click or button click) use the directory name + file name for opening:
//populating:
var files = Directory.GetFiles(YOUR_FOLDER_PATH, "*.txt");
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
//assuming ListBox:
listBox.Items.Add(filename);
}
//opening (from listbox)
var fileName = Path.Combine(YOUR_FOLDER_PATH, listBox.SelectedItem.ToString());
File.ReadAllText(fileName);
You just need a FolderBrowserDialog control.
var fileNames = new List<string>();
var fileContents = new Dictionary<string, string>();
var filePaths = Directory.EnumerateFiles(folderBrowserDialog1.SelectedPath, "*.txt");
foreach (var filePath in filePaths)
{
var fileName =new FileInfo(filePath).Name;
fileNames.Add(fileName);
fileContents.Add(fileName, File.ReadAllText(filePath));
}
I have a windows form application which works with text file data sets that I tried to make portable (ie: To run the application from external hard drive or pen drive does not need to copy the data sets into the C:\ drive directly.
I changed
StreamReader fileitem = new StreamReader("c:\\dataset.txt");
into
StreamReader fileitem = new StreamReader("dataset.txt");
and copy the dataset into the exe file path (.../bin/debug)
But it shows an error "function has stopped working"!
Any idea?
Here's a sample of how you can get the absolute path to your executable file:
static public string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return Path.GetDirectoryName(path);
}
}
Sample taken from this answer
If you implement this property you can then update your code to the following:
StreamReader fileitem = new StreamReader(AssemblyDirectory + "dataset.txt");
In the below code, the files are being saved in the debug folder of the project, I want to store the files in the appdata folder under a generic specified folder!
AViewModel vm = DataContext as AViewModel;
var table = vm.FileSelectedItem;
if (table != null)
{
var filename = System.IO.Path.GetTempFileName();
File.WriteAllBytes(table.FileTitle, table.Data);
Process prc = new Process();
prc.StartInfo.FileName = table.FileTitle;
prc.Start();
}
//table.FileTitle is the name of the file stored in the db
// eg:(test1.docx, test2.pdf, test3.txt, test4.xlsx)
//table.Data is public byte[] Data { get; set; } property
// which stores the files coming from the db.
I am looking at GetFolderPath and trying something like this now
System.IO.Path.GetTempFileName(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
Thanks for any replys!
GetTempFileName returns a full path to a file in the user's temp path. You can't use that to create a file within a specific folder.
Given that you want to store within the AppData folder already, perhaps you are after something more like:
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "YourCompany\\YourProduct\\Output");
var filename = Path.Combine(path, table.FileTitle);
File.WriteAllBytes(filename, table.Data);
Process.Start(filename);
In case you want to create randomly named file under AppData, you can try
Guid.NewGuid().ToString("N")
That'll give you random string with reasonable certainty it is unique. For folder under AppData:
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), Guid.NewGuid().ToString("N"));
Note: at least put it into some subfolder, AppData is folder shared with all other apps.
how can i assign a to a variable, which is located at the same project, for example at my project i created a folder named App_Data and for example the file is file.dat , how can i assign the file at a variable,.. for example:
var file = App_Data/file.dat
I need it to be assigned to a variable because i will be using that variable as a parameter to a method,.. it used to be :
var file= HttpContext.Current.Request.MapPath("/App_Data/file.dat");
but now i want the path to be at the same project
if it should be absolute path it should be fine too
The MapPath should give you the absolute location of the file on disk from a relative url to the root of your website:
var absoluteFileLocation = HostingEnvironment.MapPath("~/App_Data/file.dat");
This should return something like:
c:\inetpub\wwwroot\MyWebSite\App_Data\file.dat
UPDATE:
It looks like you are trying to retrieve the contents of the file, not the location. Here's how this could be done:
var absoluteFileLocation = HostingEnvironment.MapPath("~/App_Data/file.dat");
string fileContents = System.IO.File.ReadAllText(absoluteFileLocation);
You need to read the file using one of the available methods (Streams, Readers, etc).
The easiest would be:
string fileContent = File.ReadAllText(fileNameAndPath);
where the variable fileNameAndPath contains the full path and file name to the file as described by Darin Dimitrov.
Your intention isn't exactly clear, anyway:
if you want file stats:
System.IO.File file = new System.IO.File("~/App_Data/file.dat");
if you want the file content use:
public static string readFileContent(String filename)
{
try
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
return sr.ReadToEnd();
}
catch { return String.Empty; }
}