Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to read source and destination paths from a text file and use only those paths to do some operation in c#.net windows application?
Posted
Comments
Mahesh Bailwal 28-May-13 5:58am    
What is the format of path in text file? is it like source="C:\abc.txt".
OriginalGriff 28-May-13 8:32am    
If you edit other peoples answers, you should make sure that:
1) Your additions fit in with the rest of the answer.
2) Your additions are efficient and sensible.
3) Your additions do not introduce run time errors that you do not check for.
I have rolled back to undo your changes.
Mahesh Bailwal 28-May-13 8:47am    
I apologize for trouble I cause you. I thought user had mentioned “source file : \\123.456.78\abc” so to retrieve source path, string might need to split up by “:”.
Please let me know if my understanding was wrong or I made some syntax error or missed some check. So that going forward I should make those mistakes.
Thanks
OriginalGriff 28-May-13 9:01am    
I think the "source file :" part is just text, the file itself contains the path data.
But
Split(new char[]{':'})
if pretty poor coding:
Split(':')
is a lot more readable, and more efficient because it doesn't require a new object allocation.
Plus, what happens with your code if there is no ':' in the string? There will be no element 1 to access, so you get a runtime error. That's why I checked for at least two lines read back from the file...:laugh:
Mahesh Bailwal 30-May-13 1:42am    
Thanks :)

Exactly how you read them depends on how you have them stored in the file, but if they are on separate lines the easiest way is to use ReadAllLines:
C#
string[] paths = File.ReadAllLines(@"D:\Temp\myPaths.txt");
if (paths.Length >= 2)
   {
   string sourcePath = paths[0];
   string destPath = paths[1];
   ...
   }
If you then want to read from the source:
C#
string data = File.ReadAllText(sourcePath);
Or write to the destination:
C#
File.WriteAllText(destPath, data);
There may be other methods that are more appropriate, depending on what / how you want to read / write but that is up to your application.
 
Share this answer
 
v3
As suggested it depends on the text file content.
For instance if the file contains only source and destination path, on different lines, your task is really easy: read the two lines and set your application variables to their content, then perform required operation based on the values of such variables.
 
Share this answer
 
if your source and destination are in single line saperated by delimmeter then
C#
StreamReader sr = new StreamReader("Your Text File Path");
            string[] Arr;
            string line = sr.ReadLine;
            Arr=line.Split(","); //if source & destination are in same line and some delimmeter separated like "," or any ";"
            MessageBox.Show(Arr[0], "Source");
            MessageBox.Show(Arr[1], "Destination");

else if in two lines then
C#
string[] Arr = File.ReadAllLines("your text file path");
if (Arr.Length >= 2)
   {
   string source = Arr[0];
   string destination = Arr[1];
   ...
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900