Graalians

Graalians (https://www.graalians.com/forums/index.php)
-   Technology (https://www.graalians.com/forums/forumdisplay.php?f=54)
-   -   Why can't a call a different constructor?(C#) (https://www.graalians.com/forums/showthread.php?t=37577)

Skill 12-20-2016 05:11 AM

Why can't a call a different constructor?(C#)
 
So I'm trying to run the following:

PHP Code:

 string[] lines = { "First line""Second line""Third line" };
            
Windows.ApplicationModel.Package package Windows.ApplicationModel.Package.Current;
            
Windows.Storage.StorageFolder installedLocation package.InstalledLocation;
            
string myDocPath installedLocation.Path;
            
string fullPath myDocPath + @"\WriteLines.txt";
            
using (StreamWriter outputFile = new StreamWriter(fullPath))
            {
                foreach (
string line in lines)
                    
outputFile.WriteLine(line);
            } 


But I get the error "cannot convert from string to system.IO.Stream", pointing to fullPath when I try to pass it on to the constructor for StreamWriter.



According to this there should be an overloaded constructor for it to create the object via a string: https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx

So why does it tell me it expects a Stream object? Am I missing something?

Emera 12-20-2016 10:19 AM

I was able to pass a string in as a parameter successfully:
PHP Code:

var path = @"C:\Temp\aaron.txt";
using (var sw = new StreamWriter(path))
{
     
//..


Which assembly is Windows.ApplicationModel.Package located in?

It's also worth mentioning that you can get rid of that loop and replace it with a call to WriteAllLines() if all you're doing is saving the contents of your array to a file :)

PHP Code:

var path = @"C:\Temp\aaron.txt";
string[] lines = { "First line""Second line""Third line" };
System.IO.File.WriteAllLines(pathlines); 



All times are GMT. The time now is 06:19 AM.

Powered by vBulletin/Copyright ©2000 - 2024, vBulletin Solutions Inc.