Archive for the ‘.NET’ Category

  • C# 文本文件读写追加操作代码

    0

    创建文本文件

    public class FileClass

    {
    public static void Main()
    {
    WriteToFile();
    }
    static void WriteToFile()
    {
    StreamWriter SW;
    SW=File.CreateText("c:\\MyTextFile.txt");
    SW.WriteLine("God is greatest of them all");
    SW.WriteLine("This is second line");
    SW.Close();
    Console.WriteLine("File Created SucacessFully");
    }
    }

    读取文本文件


    public class FileClass
    {
    public static void Main()
    {
    ReadFromFile("c:\\MyTextFile.txt");
    }
    static void ReadFromFile(string filename)
    {
    StreamReader SR;
    string S;
    SR=File.OpenText(filename);
    S=SR.ReadLine();
    while(S!=null)
    {
    Console.WriteLine(S);
    S=SR.ReadLine();
    }
    SR.Close();
    }
    }

    追加文本


    public class FileClass
    {
    public static void Main()
    {
    AppendToFile();
    }
    static void AppendToFile()
    {
    StreamWriter SW;
    SW=File.AppendText("C:\\MyTextFile.txt");
    SW.WriteLine("This Line Is Appended");
    SW.Close();
    Console.WriteLine("Text Appended Successfully");
    }
    }

    分享到
  • c#获取文件路径 – 几种不同要求

    0

    C# 获取路径
    string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名。
    string str2=Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径。
    string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
    string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
    string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
    string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
    string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该 应用程序的目录的名称。

    “Application.StartupPath”:获取当前应用程序所在目录的路径,最后不包含“\”;
    “Application.ExecutablePath ”:获取当前应用程序文件的路径,包含文件的名称;
    “AppDomain.CurrentDomain.BaseDirectory”:获取当 前应用程序所在目录的路径,最后包含“\”;
    “System.Threading.Thread.GetDomain().BaseDirectory”: 获取当前应用程序所在目录的路径,最后包含“\”;
    “Environment.CurrentDirectory”:获取当前应用程序的路径,最 后不包含“\”;
    “System.IO.Directory.GetCurrentDirectory”:获取当前应用程序的路径,最后不包含 “\”;

    分享到
  • Force Uppercase in Textbox

    0

    txtBTAddr1.Text = txtBTAddr1.Text.ToUpper

    txtBTAddr1.Select(txtBTAddr1.Text.Length, 0)

    分享到
  • 开发人员最喜爱的十大免费的Visual Studio插件

    0

    转载自http://www.colobu.com

    1. AnkhSVN – Subversion SCC Provider

    http://ankhsvn.open.collab.net/

    AnkhSVN 是一个 VS Subversion 源代码管理提供者。通过这个插件 , 你可以方便的在 Visual Studio 中使用 Subversion 管理你的项目和代码。这个项目保持着相当的活跃性。在本文发表时最新的版本是 2.1.7444 。强烈推荐开发者使用,尤其是你将你的项目托管到 google code 或者 sourceforge 上,你可以使用这个插件连接你的项目。


    » Read the rest of the entry..

    分享到
  • ASP.NET C# Cookie读写代码

    2

    虽然不常用,不过还是要记录一下。
    以后要多学会用Cookie保存用户信息。

      public void SaveCookie()
        {
            // Use this line when you want to save a cookie
            Response.Cookies["MyCookieName"].Value = "MyCookieValue";
            // How long will cookie exist on client hard disk
            Response.Cookies["MyCookieName"].Expires = DateTime.Now.AddDays(1);
    
            // To add multiple key/value pairs in single cookie
            Response.Cookies["VisitorData"]["FirstName"] = "Richard";
            Response.Cookies["VisitorData"]["LastVisit"] = DateTime.Now.ToString();
        }
    
     » Read the rest of the entry.. 
    分享到
  • 文本框回车触发,文本框内容变大写。

    0

    将文本框内容自动转为大写。

    虽然只有一行代码,但是不知道就是做不了

    private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    //将用户输入的小写字母转成大写的形式
    e.KeyChar = char.ToUpper(e.KeyChar);
    }

    文本框回车触发事件

    这个没有什么难度,就是不记得……

            private void TextBox_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyValue == 13)
                {
                    Search_Click();
                }
            }
    分享到
  • C# 创建TXT文本日志,在尾行追加内容

    0

    调试程序总是会用需要一个日志文件记录调试过程。这个代码会自动创建一个文本文件然后在尾行添加新的内容。

    可能会存在的问题是:如果这个日志已经被一个用户打开,可能其他用户就不能写入了。不过我用了

     using (StreamWriter SW = File.AppendText(LogFile))

    来解决这个问题。但是没有进行完全性的测试。
    » Read the rest of the entry..

    分享到