Reading lines from text file and add to List
I'm working in a project that I need read lines in a .txt file and push this lines into a List.
In my first prototype, in WPF, I used this very simple code to do this:
private static List<string> fileLines = File.ReadAllLines("WordList.txt").ToList();
However, now I am working at a Windows Phone 8.1 version and I can't do that in this way.
For now, my research come this code but no successfully:
public static List<string> fileLines = LoadingLines();
public static async Task<List<string>> LoadingLines()
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("WordList.txt");
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
return text;
}
Some problem that I have with this code is that I don't know if this returns a List where each line of the file is one element and if I try assign a Task> type to a List I have this error: Error 2 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.List'
Maybe this code are totally wrong to what I need. Again, what I need is just read a .txt file in my packaged app and turn each line in an element of fileLines List. Obviously, after, I need access this List out of the method.
c# windows
add a comment |
I'm working in a project that I need read lines in a .txt file and push this lines into a List.
In my first prototype, in WPF, I used this very simple code to do this:
private static List<string> fileLines = File.ReadAllLines("WordList.txt").ToList();
However, now I am working at a Windows Phone 8.1 version and I can't do that in this way.
For now, my research come this code but no successfully:
public static List<string> fileLines = LoadingLines();
public static async Task<List<string>> LoadingLines()
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("WordList.txt");
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
return text;
}
Some problem that I have with this code is that I don't know if this returns a List where each line of the file is one element and if I try assign a Task> type to a List I have this error: Error 2 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.List'
Maybe this code are totally wrong to what I need. Again, what I need is just read a .txt file in my packaged app and turn each line in an element of fileLines List. Obviously, after, I need access this List out of the method.
c# windows
1
What exactly doesno successfully
mean? What failed and where?
– Der Kommissar
Apr 30 '15 at 18:36
2
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37
add a comment |
I'm working in a project that I need read lines in a .txt file and push this lines into a List.
In my first prototype, in WPF, I used this very simple code to do this:
private static List<string> fileLines = File.ReadAllLines("WordList.txt").ToList();
However, now I am working at a Windows Phone 8.1 version and I can't do that in this way.
For now, my research come this code but no successfully:
public static List<string> fileLines = LoadingLines();
public static async Task<List<string>> LoadingLines()
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("WordList.txt");
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
return text;
}
Some problem that I have with this code is that I don't know if this returns a List where each line of the file is one element and if I try assign a Task> type to a List I have this error: Error 2 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.List'
Maybe this code are totally wrong to what I need. Again, what I need is just read a .txt file in my packaged app and turn each line in an element of fileLines List. Obviously, after, I need access this List out of the method.
c# windows
I'm working in a project that I need read lines in a .txt file and push this lines into a List.
In my first prototype, in WPF, I used this very simple code to do this:
private static List<string> fileLines = File.ReadAllLines("WordList.txt").ToList();
However, now I am working at a Windows Phone 8.1 version and I can't do that in this way.
For now, my research come this code but no successfully:
public static List<string> fileLines = LoadingLines();
public static async Task<List<string>> LoadingLines()
{
StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile sampleFile = await storageFolder.GetFileAsync("WordList.txt");
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
return text;
}
Some problem that I have with this code is that I don't know if this returns a List where each line of the file is one element and if I try assign a Task> type to a List I have this error: Error 2 Cannot implicitly convert type 'System.Threading.Tasks.Task>' to 'System.Collections.Generic.List'
Maybe this code are totally wrong to what I need. Again, what I need is just read a .txt file in my packaged app and turn each line in an element of fileLines List. Obviously, after, I need access this List out of the method.
c# windows
c# windows
edited Apr 30 '15 at 21:19
Tiago Zontag
asked Apr 30 '15 at 18:34
Tiago ZontagTiago Zontag
11
11
1
What exactly doesno successfully
mean? What failed and where?
– Der Kommissar
Apr 30 '15 at 18:36
2
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37
add a comment |
1
What exactly doesno successfully
mean? What failed and where?
– Der Kommissar
Apr 30 '15 at 18:36
2
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37
1
1
What exactly does
no successfully
mean? What failed and where?– Der Kommissar
Apr 30 '15 at 18:36
What exactly does
no successfully
mean? What failed and where?– Der Kommissar
Apr 30 '15 at 18:36
2
2
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37
add a comment |
2 Answers
2
active
oldest
votes
According to the documentation a call to ReadLinesAsync
will return IAsyncOperation<IVector>
, i.e. when you await it, this will return an IVector<string>
. For this type, documentation states:
Represents a random-access collection of elements. .NET: This
interface appears to .NET code asSystem.Collections.Generic.IList<T>
due to .NET language projection. In any case where a Windows Runtime
type has implemented IVector, .NET code can use the APIs of
IList<T>
instead.
So this line in your code should work fine
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
The problem is that text
is of the type IList<string>
, but the return type for your method is List<string>
. You cannot assign an IList<string>
instance to the return value of type List<string>
.
To fix this, you can use one of the following two options:
Change the signature of your method to
public static async Task<IList<string>> LoadingLines()
Convert your methods result to a
List<string>
when you return it:
return text.ToList();
When you use the result returned from your method, you also must do so with the await
keyword, so this line:
public static List<string> fileLines = LoadingLines();
Will not work correctly, because LoadingLines
returns a Task<List<string>>
. You cannot asynchronously load into a static member variable. If you want this to work (making minimal changes to your code)1 what you can do is the following:
Remove this line:
public static List<string> fileLines = LoadingLines();
And replace it with:
private readonly static Task<List<string>> loadLinesTask = LoadingLines();
public static async Task<List<string>> GetLinesAsync()
{
return await _loadLinesTask.ConfigureAwait(false);
}
You can then call GetLinesAsync()
in other "async" methods. If you need to call this in methods that are not marked as async
, you cannot use await
and (using MyClass
for the class with these methods) you will have to do this to get the list of lines:
var lines = MyClass.GetLinesAsync().Result;
1 There are better ways to do this, but to discuss that that would go beyond the scope of this question.
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
|
show 1 more comment
Add the text file to your project. Right-click it, choose Properties and set the action property to "embedded resource".
Access it as follows. The resource name is fully qualified name, meaning the namespace is included. (Ex. - "MyCompany.Project.Resouces.fileName.txt").
var list = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XYZ.ABC.Resources.MyTextFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
return list;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29975219%2freading-lines-from-text-file-and-add-to-liststring%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to the documentation a call to ReadLinesAsync
will return IAsyncOperation<IVector>
, i.e. when you await it, this will return an IVector<string>
. For this type, documentation states:
Represents a random-access collection of elements. .NET: This
interface appears to .NET code asSystem.Collections.Generic.IList<T>
due to .NET language projection. In any case where a Windows Runtime
type has implemented IVector, .NET code can use the APIs of
IList<T>
instead.
So this line in your code should work fine
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
The problem is that text
is of the type IList<string>
, but the return type for your method is List<string>
. You cannot assign an IList<string>
instance to the return value of type List<string>
.
To fix this, you can use one of the following two options:
Change the signature of your method to
public static async Task<IList<string>> LoadingLines()
Convert your methods result to a
List<string>
when you return it:
return text.ToList();
When you use the result returned from your method, you also must do so with the await
keyword, so this line:
public static List<string> fileLines = LoadingLines();
Will not work correctly, because LoadingLines
returns a Task<List<string>>
. You cannot asynchronously load into a static member variable. If you want this to work (making minimal changes to your code)1 what you can do is the following:
Remove this line:
public static List<string> fileLines = LoadingLines();
And replace it with:
private readonly static Task<List<string>> loadLinesTask = LoadingLines();
public static async Task<List<string>> GetLinesAsync()
{
return await _loadLinesTask.ConfigureAwait(false);
}
You can then call GetLinesAsync()
in other "async" methods. If you need to call this in methods that are not marked as async
, you cannot use await
and (using MyClass
for the class with these methods) you will have to do this to get the list of lines:
var lines = MyClass.GetLinesAsync().Result;
1 There are better ways to do this, but to discuss that that would go beyond the scope of this question.
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
|
show 1 more comment
According to the documentation a call to ReadLinesAsync
will return IAsyncOperation<IVector>
, i.e. when you await it, this will return an IVector<string>
. For this type, documentation states:
Represents a random-access collection of elements. .NET: This
interface appears to .NET code asSystem.Collections.Generic.IList<T>
due to .NET language projection. In any case where a Windows Runtime
type has implemented IVector, .NET code can use the APIs of
IList<T>
instead.
So this line in your code should work fine
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
The problem is that text
is of the type IList<string>
, but the return type for your method is List<string>
. You cannot assign an IList<string>
instance to the return value of type List<string>
.
To fix this, you can use one of the following two options:
Change the signature of your method to
public static async Task<IList<string>> LoadingLines()
Convert your methods result to a
List<string>
when you return it:
return text.ToList();
When you use the result returned from your method, you also must do so with the await
keyword, so this line:
public static List<string> fileLines = LoadingLines();
Will not work correctly, because LoadingLines
returns a Task<List<string>>
. You cannot asynchronously load into a static member variable. If you want this to work (making minimal changes to your code)1 what you can do is the following:
Remove this line:
public static List<string> fileLines = LoadingLines();
And replace it with:
private readonly static Task<List<string>> loadLinesTask = LoadingLines();
public static async Task<List<string>> GetLinesAsync()
{
return await _loadLinesTask.ConfigureAwait(false);
}
You can then call GetLinesAsync()
in other "async" methods. If you need to call this in methods that are not marked as async
, you cannot use await
and (using MyClass
for the class with these methods) you will have to do this to get the list of lines:
var lines = MyClass.GetLinesAsync().Result;
1 There are better ways to do this, but to discuss that that would go beyond the scope of this question.
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
|
show 1 more comment
According to the documentation a call to ReadLinesAsync
will return IAsyncOperation<IVector>
, i.e. when you await it, this will return an IVector<string>
. For this type, documentation states:
Represents a random-access collection of elements. .NET: This
interface appears to .NET code asSystem.Collections.Generic.IList<T>
due to .NET language projection. In any case where a Windows Runtime
type has implemented IVector, .NET code can use the APIs of
IList<T>
instead.
So this line in your code should work fine
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
The problem is that text
is of the type IList<string>
, but the return type for your method is List<string>
. You cannot assign an IList<string>
instance to the return value of type List<string>
.
To fix this, you can use one of the following two options:
Change the signature of your method to
public static async Task<IList<string>> LoadingLines()
Convert your methods result to a
List<string>
when you return it:
return text.ToList();
When you use the result returned from your method, you also must do so with the await
keyword, so this line:
public static List<string> fileLines = LoadingLines();
Will not work correctly, because LoadingLines
returns a Task<List<string>>
. You cannot asynchronously load into a static member variable. If you want this to work (making minimal changes to your code)1 what you can do is the following:
Remove this line:
public static List<string> fileLines = LoadingLines();
And replace it with:
private readonly static Task<List<string>> loadLinesTask = LoadingLines();
public static async Task<List<string>> GetLinesAsync()
{
return await _loadLinesTask.ConfigureAwait(false);
}
You can then call GetLinesAsync()
in other "async" methods. If you need to call this in methods that are not marked as async
, you cannot use await
and (using MyClass
for the class with these methods) you will have to do this to get the list of lines:
var lines = MyClass.GetLinesAsync().Result;
1 There are better ways to do this, but to discuss that that would go beyond the scope of this question.
According to the documentation a call to ReadLinesAsync
will return IAsyncOperation<IVector>
, i.e. when you await it, this will return an IVector<string>
. For this type, documentation states:
Represents a random-access collection of elements. .NET: This
interface appears to .NET code asSystem.Collections.Generic.IList<T>
due to .NET language projection. In any case where a Windows Runtime
type has implemented IVector, .NET code can use the APIs of
IList<T>
instead.
So this line in your code should work fine
IList<string> text = await Windows.Storage.FileIO.ReadLinesAsync(sampleFile);
The problem is that text
is of the type IList<string>
, but the return type for your method is List<string>
. You cannot assign an IList<string>
instance to the return value of type List<string>
.
To fix this, you can use one of the following two options:
Change the signature of your method to
public static async Task<IList<string>> LoadingLines()
Convert your methods result to a
List<string>
when you return it:
return text.ToList();
When you use the result returned from your method, you also must do so with the await
keyword, so this line:
public static List<string> fileLines = LoadingLines();
Will not work correctly, because LoadingLines
returns a Task<List<string>>
. You cannot asynchronously load into a static member variable. If you want this to work (making minimal changes to your code)1 what you can do is the following:
Remove this line:
public static List<string> fileLines = LoadingLines();
And replace it with:
private readonly static Task<List<string>> loadLinesTask = LoadingLines();
public static async Task<List<string>> GetLinesAsync()
{
return await _loadLinesTask.ConfigureAwait(false);
}
You can then call GetLinesAsync()
in other "async" methods. If you need to call this in methods that are not marked as async
, you cannot use await
and (using MyClass
for the class with these methods) you will have to do this to get the list of lines:
var lines = MyClass.GetLinesAsync().Result;
1 There are better ways to do this, but to discuss that that would go beyond the scope of this question.
edited Apr 30 '15 at 21:50
answered Apr 30 '15 at 18:50
AlexAlex
11.1k2148
11.1k2148
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
|
show 1 more comment
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
@TiagoZontag, you are welcome. If this answered your question, you may want to mark it as the answer, so that your question can be removed from the "unanswered questions" list. See What should I do when someone answers my question?
– Alex
Apr 30 '15 at 21:00
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
Alex, thank you but not works because I need assign the return value to the List<string> fileLines. Some another conversion remains needed: Cannot implicitly convert type 'System.Threading.Tasks.Task<System.Collections.Generic.List<string>>' to 'System.Collections.Generic.List<string>'
– Tiago Zontag
Apr 30 '15 at 21:03
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
You're getting a task as a result, assign the .Result() of the object instead. If your method has the async modifier as suggested in the answer, await it - var result = await LoadingLines()
– kidshaw
Apr 30 '15 at 21:17
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
@TiagoZontag I added additional information on the second error you are getting, and how you can solve that.
– Alex
Apr 30 '15 at 21:51
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
Thank you @Alex, but I will continue looking for this better way because I think that what I have until now are very bad. Just say me if I can do that without async method. Thank you again.
– Tiago Zontag
May 1 '15 at 19:13
|
show 1 more comment
Add the text file to your project. Right-click it, choose Properties and set the action property to "embedded resource".
Access it as follows. The resource name is fully qualified name, meaning the namespace is included. (Ex. - "MyCompany.Project.Resouces.fileName.txt").
var list = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XYZ.ABC.Resources.MyTextFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
return list;
add a comment |
Add the text file to your project. Right-click it, choose Properties and set the action property to "embedded resource".
Access it as follows. The resource name is fully qualified name, meaning the namespace is included. (Ex. - "MyCompany.Project.Resouces.fileName.txt").
var list = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XYZ.ABC.Resources.MyTextFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
return list;
add a comment |
Add the text file to your project. Right-click it, choose Properties and set the action property to "embedded resource".
Access it as follows. The resource name is fully qualified name, meaning the namespace is included. (Ex. - "MyCompany.Project.Resouces.fileName.txt").
var list = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XYZ.ABC.Resources.MyTextFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
return list;
Add the text file to your project. Right-click it, choose Properties and set the action property to "embedded resource".
Access it as follows. The resource name is fully qualified name, meaning the namespace is included. (Ex. - "MyCompany.Project.Resouces.fileName.txt").
var list = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "XYZ.ABC.Resources.MyTextFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
while (!reader.EndOfStream)
{
list.Add(reader.ReadLine());
}
}
return list;
answered Nov 21 '18 at 21:53
JM.JM.
4581020
4581020
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f29975219%2freading-lines-from-text-file-and-add-to-liststring%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What exactly does
no successfully
mean? What failed and where?– Der Kommissar
Apr 30 '15 at 18:36
2
What exactly is not working as you expected. Do you get any errors, is the result different from what you expected? Please edit your question to add the relevant details that may allow us to help you find out what is wrong and how to resolve it.
– Alex
Apr 30 '15 at 18:37