Some C# questions

Show off new creations, get help, or just chat about all aspects of web development and design.

Some C# questions

Postby Spike on Tue Mar 31, 2009 5:56 pm

I'm trying to get into a string the first line of a .txt file. But I'm getting an error.

Code: Select all
        private void Form1_Shown(object sender, EventArgs e)
        {
            FileStream istream;
            try
            {
                istream = new FileStream("config.txt", FileMode.OpenOrCreate, FileAccess.Read);
            }
            catch (Exception)
            {
            }

            // Associate a reader with the stream
            StreamReader reader = new StreamReader(istream);

            string text = reader.ReadLine();

            reader.Close();
        }


Error is in spanish but it's something like: "Use of the local variable not assigned 'istream' " on this line: StreamReader reader = new StreamReader(istream);
Last edited by Spike on Thu Apr 02, 2009 8:12 pm, edited 1 time in total.
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: C Sharp IO problem

Postby zombie@computer on Tue Mar 31, 2009 6:40 pm

It seems the FileStream file opening is failing. The idea of the try{} catch{} is that you handle exceptions in the catch statement. This code allows an exception, then continues with variables that may not have been set.

Depending on what you want, File.ReadAllLines() may be easier
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: C Sharp IO problem

Postby Haxel on Wed Apr 01, 2009 9:56 pm

the compile error is just because istream isnt defined as anything in the same scope as where it's being used, try

FileStream istream = null;
User avatar
Haxel
Sir Post-a-lot
Sir Post-a-lot
 
Joined: Fri Oct 08, 2004 4:36 pm
Location: UK

Re: C Sharp IO problem

Postby ankhsethamon on Thu Apr 02, 2009 5:54 am

Sorry.. my post is not helpfull.. XD I'm just curious... why is the error message in spanish? ( where r u from? XD )
Image
User avatar
ankhsethamon
Been Here A While
Been Here A While
 
Joined: Sat Aug 09, 2008 8:28 pm
Location: Mexico

Re: C Sharp IO problem

Postby zombie@computer on Thu Apr 02, 2009 6:46 pm

Haxel wrote:the compile error is just because istream isnt defined as anything in the same scope as where it's being used, try

FileStream istream = null;

if the error was something like error, trying to use uninitialized variable 'istream' then i think you are right.
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: C Sharp IO problem

Postby Spike on Thu Apr 02, 2009 6:55 pm

I fixed it by initialicing the variable (yeah I always forget to do it on C#).

@ankhsethamon: lol I'm spanish that's why.
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: C Sharp IO problem

Postby ankhsethamon on Thu Apr 02, 2009 7:44 pm

Spike wrote:@ankhsethamon: lol I'm spanish that's why.


:P ya veo.. eso explica muchas cosas.. XD jeje. Algo así me imaginaba.
Image
User avatar
ankhsethamon
Been Here A While
Been Here A While
 
Joined: Sat Aug 09, 2008 8:28 pm
Location: Mexico

Re: C Sharp IO problem

Postby Spike on Thu Apr 02, 2009 8:11 pm

ankhsethamon wrote:
Spike wrote:@ankhsethamon: lol I'm spanish that's why.


:P ya veo.. eso explica muchas cosas.. XD jeje. Algo así me imaginaba.

Lol you're spanish too? Maybe you've seen me on HL2spain...
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: Some C# questions

Postby Spike on Thu Apr 02, 2009 8:17 pm

I'm changing the title of the thread because I have more questions to do, and I don't know how many I'll have. I haven't founf any answer on google.

-I have the path of a file on a string, I know there is a simple way to get the file name from the file path (string Name = System.IO.Path.GetFileName(FilePath)), but now, I would like to go backwards from a path. So if I have C:/Folder1/Folder2 in a string, I would like to get C:/Folder1.

-I want to read a .VMT file as text, how do I have to open it? As a Binary file? Are VMT characters in ASCII or Unicode?
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: Some C# questions

Postby ankhsethamon on Thu Apr 02, 2009 8:50 pm

Spike wrote:Lol you're spanish too? Maybe you've seen me on HL2spain...


hehe.. ^^ in fact I'm from México :P

Spike wrote:...but now, I would like to go backwards from a path. So if I have C:/Folder1/Folder2 in a string, I would like to get C:/Folder1.


try adding "/../" at the end of your path... ".." means "one step back" in the directory hierarchy.


Code: Select all

fileDir = System.IO.Path.GetDirectoryName(filePath);
previousDir = fileDir + "/../";

 


I'm not pretty sure about this... let me know if works! :)

Saludos XD
Image
User avatar
ankhsethamon
Been Here A While
Been Here A While
 
Joined: Sat Aug 09, 2008 8:28 pm
Location: Mexico

Re: Some C# questions

Postby zombie@computer on Thu Apr 02, 2009 9:02 pm

Spike wrote:I'm changing the title of the thread because I have more questions to do, and I don't know how many I'll have. I haven't founf any answer on google.

-I have the path of a file on a string, I know there is a simple way to get the file name from the file path (string Name = System.IO.Path.GetFileName(FilePath)), but now, I would like to go backwards from a path. So if I have C:/Folder1/Folder2 in a string, I would like to get C:/Folder1.

-I want to read a .VMT file as text, how do I have to open it? As a Binary file? Are VMT characters in ASCII or Unicode?
string result = FilePath.substring(0,FilePath.trimend('/').lastindexof('/')+1);

-file.readalltext or file.readalllines works best
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Some C# questions

Postby Spike on Thu Apr 02, 2009 9:17 pm

THNX :D

you well deserved the award :P
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: Some C# questions

Postby Spike on Sun Apr 05, 2009 10:04 pm

I've got another question. I've a .txt file with lots of writen lines, I would like to arrange all the file by alphabet. Is there any easy way of doing it?
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Re: Some C# questions

Postby zombie@computer on Mon Apr 06, 2009 7:22 pm

Spike wrote:I've got another question. I've a .txt file with lots of writen lines, I would like to arrange all the file by alphabet. Is there any easy way of doing it?

if you use File.ReadAllLines() :

string[] lines = File.ReadAllLines(...);
Array.Sort(lines);
When you are up to your neck in shit, keep your head up high
zombie@computer
Forum Goer Elite™
Forum Goer Elite™
 
Joined: Fri Dec 31, 2004 5:58 pm
Location: Lent, Netherlands

Re: Some C# questions

Postby Spike on Tue Apr 07, 2009 10:40 pm

thnx:D
User avatar
Spike
May Contain Skills
May Contain Skills
 
Joined: Wed Sep 17, 2008 7:10 pm

Return to Web Design & Development

Who is online

Users browsing this forum: No registered users

cron