1. 程式人生 > >Revit二次開發之WPF通過txt讀取和儲存TextBox的字串【附原始碼】

Revit二次開發之WPF通過txt讀取和儲存TextBox的字串【附原始碼】

軟體版本:VS2015 Revit2018

功能:Revit中執行程式時,在Window中的TextBox中自動顯示指定txt檔案中的字串內容

缺點:會將txt檔案中的字串全部顯示

程式展示:

1.程式啟動


2.輸入“666”,點選Button1,自動關閉窗體


3.再次啟動程式,刪除“666”,輸入“777”,點選Button1,自動關閉窗體


4.再次啟動窗體,發現TextBox內會同時顯示兩次的輸入值。開啟txt檔案進行檢視。



7.具體程式


<Window x:Class="Test.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d"
        Title="Window1" Height="350" Width="525">
    <Grid>
        <TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="55" Margin="146,108,0,0" TextWrapping="Wrap" Text="{Binding Path=SSS,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/>
        <Button x:Name="button1" Content="Button1" HorizontalAlignment="Left" Margin="198,186,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>

    </Grid>
</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;//新增引用
using System.ComponentModel;

namespace Test
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            DialogResult = true;
            WriteFile("E:\\123.txt", textBox1.Text);
            Close();
        }
        /// <summary>
        /// 寫檔案
        /// </summary>
        /// <param name="Path">檔案路徑</param>
        /// <param name="Strings">檔案內容</param>
        public static void WriteFile(string Path, string Strings)
        {
            if (!System.IO.File.Exists(Path))
            {
                //Directory.CreateDirectory(Path);
                System.IO.FileStream f = System.IO.File.Create(Path);
                f.Close();
                f.Dispose();
            }
            System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.UTF8);
            f2.WriteLine(Strings);
            f2.Close();
            f2.Dispose();
        }

        private static string ReadFile(string Path)
        {
            //宣告一個FileStream類的物件  
            FileStream fsRead = new FileStream(Path, FileMode.OpenOrCreate, FileAccess.Read);
            byte[] buffer = new byte[200];//宣告一個位元組陣列,用來臨時儲存讀取到資料,最大儲存200位元組  
            string a = null;
            while (true)
            {
                int r = fsRead.Read(buffer, 0, buffer.Length);//返回本次實際讀取到的位元組數   
                if (r == 0)//如果讀取到的位元組數為0,說明已到達檔案結尾,則退出while迴圈  
                {
                    break;
                }
                string s = Encoding.UTF8.GetString(buffer, 0, r);//將位元組陣列轉換成字串;buffer:要轉換的位元組陣列;0:第一個要解碼的位元組的索引;r:要解碼的位元組數   
                Console.WriteLine(s);
                a = s;
            }
            return a;
            fsRead.Close();  //關閉流  
            fsRead.Dispose(); //釋放流  
            Console.ReadKey();
        }

        public class ViewModel : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
            private string sss = ReadFile("E:\\123.txt");
            public string SSS
            {
                get { return sss; }
                set
                {
                    sss = value;
                    NotifyPropertyChanged("管道偏移高度");
                }
            }

            private void NotifyPropertyChanged(string name)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

using Autodesk.Revit.UI.Selection;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.DB.Plumbing;
using Autodesk.Revit.DB.Electrical;
using System.Windows;
using System.ComponentModel;
using static Test.Window1;

namespace Test
{
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
    class Command : IExternalCommand
    {
        UIDocument uidoc;
        Document doc;
        public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            uidoc = commandData.Application.ActiveUIDocument;
            doc = uidoc.Document;
            Window1 w1 = new Window1();
            ViewModel v1 = new ViewModel();
            w1.DataContext = v1;

            if (!w1.ShowDialog() ?? false)
                return Result.Cancelled;

            string data = v1.SSS;

            return Result.Succeeded;
        }
    }
}