心思世界's Archiver

tom1991 發表於 2008-9-12 18:21

c#怎樣能叫電腦開.txt檔?

如題...
我開c#,寫好.txt的內容,然後叫c#程式幫我開一個.txt檔,同埋係txt檔入面寫字...
有這個功能嗎:onion126

cliff 發表於 2008-9-12 18:43


using System.IO;
private void save(string f)
        {
            FileStream stream = null;
            try
            {
                stream = new FileStream(f, FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(tmp);
                writer.Flush();
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
        }

============
一個小 function, f 係 filename (string), tmp (string) 係要寫入既野

你可以自己改下~~

cliff 發表於 2008-9-12 18:44

streamwriter 係用黎寫, streamreader就係用黎讀~

tom1991 發表於 2008-9-12 18:56

萬分謝意!!!
thx~~
我試下用先

tom1991 發表於 2008-9-12 19:09

e...點解唔見個"Main" ge?

同埋電腦罵我...:onion036

[[i] 本帖最後由 tom1991 於 2008-9-12 19:13 編輯 [/i]]

tom1991 發表於 2008-9-12 19:49

另外,我剛才那個i_love_you怎樣才能自動說下去,不停的呢??有方法弄的嗎?:onion010

cliff 發表於 2008-9-12 21:28

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 19:09 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559160&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]
e...點解唔見個"Main" ge?

同埋電腦罵我...:onion036 [/quote]
我果個唔係完整part, 因為我在我一個windows app copy給你看的
使用我那個 function的條件是, 在main 內 call save(filename)
另外有一個 global variable 叫 tmp 用來 save 那些要save的字
如果還是看不懂我再給你寫個example

cliff 發表於 2008-9-12 21:31

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 19:49 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559174&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]
另外,我剛才那個i_love_you怎樣才能自動說下去,不停的呢??有方法弄的嗎?:onion010 [/quote]
你那個 iloveyou不是一直說下去嗎?

你在 new say() 內有new say(), 即是每當 new say() 這個object created, 就會再create 下一個new say()

問題是, 如果只當小現意, 這個沒甚麼問題, 如果要是惡作劇, 這個就不怎麼樣, 因為 只要 按 x 就可以關閉
或 alt + f4 都可以

tom1991 發表於 2008-9-12 21:37

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 21:28 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559246&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]

我果個唔係完整part, 因為我在我一個windows app copy給你看的
使用我那個 function的條件是, 在main 內 call save(filename)
另外有一個 global variable 叫 tmp 用來 save 那些要save的字
如果還是看不懂我再 ... [/quote]好喔,我要例子看看..謝謝:onion067

tom1991 發表於 2008-9-12 21:40

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 21:31 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559249&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]

你那個 iloveyou不是一直說下去嗎?

你在 new say() 內有new say(), 即是每當 new say() 這個object created, 就會再create 下一個new say()

問題是, 如果只當小現意, 這個沒甚麼問題, 如果要是惡作劇, 這個就 ... [/quote]沒阿,佢只是唯持10秒左右..:onion099

cliff 發表於 2008-9-12 22:07

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 21:37 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559261&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]
好喔,我要例子看看..謝謝:onion067 [/quote]
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace readwrite
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Read from a file called: data.txt\n");
            try
            {
                using (StreamReader sr = new StreamReader("data.txt"))
                {
                    string line;

                    while ((line = sr.ReadLine()) != null)
                    {
                        Console.WriteLine(line);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot read file.\n\n");
            }
            

            Console.WriteLine("\nNow the reading is done.\nEnter something for me to write into new.txt\n");
            string tmp = Console.ReadLine();
            FileStream stream = null;
            try
            {
                stream = new FileStream("new.txt", FileMode.Create);
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(tmp);
                writer.Flush();
            }
            finally
            {
                if (stream != null)
                    stream.Close();
            }
            Console.WriteLine("\nI wrote into new.txt");
            Console.ReadLine();
        }

    }
}

我上傳左成個 solution 上黎, VS 2005
基本上個program會先讀一個 data.txt 既內容, 之後 write 出黎
then 問你入d字, 之後再述下 new.txt

睇唔明我再解釋

cliff 發表於 2008-9-12 22:13

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 21:40 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559268&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]
沒阿,佢只是唯持10秒左右..:onion099 [/quote]
我重新試過, 沒有問題, over 10 秒, 會不會是你 pc 的問題
為免非香港人看不明白, 我還是用書面語回答你吧

首先你要明白一點, 當你 new say() 的時候, 就會呼叫自己多一次, 所以你的main內也無需要加入太多 new say()
另外, 這個program 除了 for fun, 只能說沒有甚麼大作用, 建議還是寫另一個再研究吧~

ivanyung 發表於 2008-9-12 22:35

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 22:13 發表 [url=http://www.cnet.hk/bbs/redirect.php?goto=findpost&pid=559318&ptid=60985][img]http://www.cnet.hk/bbs/images/common/back.gif[/img][/url]

我重新試過, 沒有問題, over 10 秒, 會不會是你 pc 的問題
[/quote]

應該不是 pc 有問題, 相信 tom 是在 C# 內直接 run, visual studio 發現無限回圈而將 program 停止 :onion126

cliff 發表於 2008-9-12 22:50

[quote]原帖由 [i]ivanyung[/i] 於 2008-9-12 22:35 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559341&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]


應該不是 pc 有問題, 相信 tom 是在 C# 內直接 run, visual studio 發現無限回圈而將 program 停止 :onion126 [/quote]
唔怪得我無問題啦, 仲以為 tom 既 pc 問題tim~~

ivanyung 發表於 2008-9-12 23:00

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 22:50 發表 [url=http://www.cnet.hk/bbs/redirect.php?goto=findpost&pid=559360&ptid=60985][img]http://www.cnet.hk/bbs/images/common/back.gif[/img][/url]

唔怪得我無問題啦, 仲以為 tom 既 pc 問題tim~~ [/quote]
我多係估下咋 , 其實我多唔識 c# :onion101
tom 咁快就寫 recursive 程式 :onion010

tom1991 發表於 2008-9-12 23:12

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 22:13 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559318&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]

我重新試過, 沒有問題, over 10 秒, 會不會是你 pc 的問題
為免非香港人看不明白, 我還是用書面語回答你吧

首先你要明白一點, 當你 new say() 的時候, 就會呼叫自己多一次, 所以你的main內也無需要加入太多 ne ... [/quote]我今次用黎for fun嫁ja...
今星期先岩岩學= =":onion036

tom1991 發表於 2008-9-12 23:23

[quote]原帖由 [i]cliff[/i] 於 2008-9-12 22:07 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559311&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace readwrite
{
    class Program
    {
        static void Main(string[] args)
        {
       ... [/quote]能用了,我慢慢研究了:onion164
謝謝:onion121

tom1991 發表於 2008-9-12 23:23

[quote]原帖由 [i]ivanyung[/i] 於 2008-9-12 22:35 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559341&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]


應該不是 pc 有問題, 相信 tom 是在 C# 內直接 run, visual studio 發現無限回圈而將 program 停止 :onion126 [/quote]是阿,,你正話run果個programe有冇自動停左?:onion082

圖:我沒按停他的,他自己停了...

[[i] 本帖最後由 tom1991 於 2008-9-12 23:34 編輯 [/i]]

tom1991 發表於 2008-9-12 23:24

[quote]原帖由 [i]ivanyung[/i] 於 2008-9-12 23:00 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559371&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]

我多係估下咋 , 其實我多唔識 c# :onion101
tom 咁快就寫 recursive 程式 :onion010 [/quote]嘻嘻,我試下嫁ja....試左都幾耐...:onion107 先試到出黎:onion120

ivanyung 發表於 2008-9-12 23:39

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 23:23 發表 [url=http://www.cnet.hk/bbs/redirect.php?goto=findpost&pid=559401&ptid=60985][img]http://www.cnet.hk/bbs/images/common/back.gif[/img][/url]
是阿,,你正話run果個programe有冇自動停左?:onion082

圖:我沒按停他的,他自己停了... [/quote]

我這裡沒有自動停止 ...
因為 complier check 到 logic error ........ :onion011

ivanyung 發表於 2008-9-12 23:40

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 23:24 發表 [url=http://www.cnet.hk/bbs/redirect.php?goto=findpost&pid=559402&ptid=60985][img]http://www.cnet.hk/bbs/images/common/back.gif[/img][/url]
嘻嘻,我試下嫁ja....試左都幾耐...:onion107 先試到出黎:onion120 [/quote]

學習過程是會經過不斷的失敗 ... :onion004

tom1991 發表於 2008-9-12 23:55

[quote]原帖由 [i]ivanyung[/i] 於 2008-9-12 23:39 發表 [url=http://cnet.hk/bbs/redirect.php?goto=findpost&pid=559426&ptid=60985][img]http://cnet.hk/bbs/images/common/back.gif[/img][/url]


我這裡沒有自動停止 ...
因為 complier check 到 logic error ........ :onion011 [/quote] check 到 logic error 但點解唔會自動幫你停ge= =:onion120

tom1991 發表於 2008-9-12 23:57

C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\[color=magenta]bin\[/color][color=blue]Release\[/color]readwrite
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\[color=magenta]bin\[/color][color=dimgray]Debug\[/color]readwrite
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\[color=seagreen]obj\[/color][color=blue]Debug\[/color]readwrite
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\[color=seagreen]obj\[/color][color=gray]Release\[/color]readwrite

*檔案名:readwrite

我應該用邊個開先正宗?

ivanyung 發表於 2008-9-13 06:52

[quote]原帖由 [i]tom1991[/i] 於 2008-9-12 23:57 發表 [url=http://www.cnet.hk/bbs/redirect.php?goto=findpost&pid=559456&ptid=60985][img]http://www.cnet.hk/bbs/images/common/back.gif[/img][/url]
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\bin\Release\readwrite
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\bin\Debug\readwrite
C:\Documents and Settings\Tom\桌面\pro ... [/quote]
this one :onion004
C:\Documents and Settings\Tom\桌面\pro\readwrite\readwrite\[color=magenta]bin\[/color][color=blue]Release\[/color]readwrite[size=0px], t# F! G0 J' W% k; C[/size]

頁: [1] 2 3 4
eXTReMe Tracker

Powered by Discuz! Archiver 6.1.0F  © 2001-2007 Comsenz Inc.