c# wpf File.WriteAllText issue

Zaug 306 Reputation points
2022-04-12T12:08:28.707+00:00

hello. first i using translate, if have mispellings on the sentences, sugges me...
i work a project with vs2022 and i save a file on a folder.
i write this code for overwrite a file:
using (StreamWriter sw = new StreamWriter(System.AppDomain.CurrentDomain.BaseDirectory + "projectTemplateSource/projekonumveadi.txt"))
{
sw.Write(projectfolder + @"\" + projectname + ".idea");
}

but this not worked on me. Not Save it.

i tried another code for overwrite a file:

File.WriteAllText(System.AppDomain.CurrentDomain.BaseDirectory + "projectTemplateSource/projekonumveadi.txt",  projectfolder + @"\" + projectname + ".idea");

but not worked again.

i already have a file. but can not overwrite it.

i tried to if exists, delete it and write again but not worked again.

can someone help me?

Developer technologies XAML
{count} votes

Accepted answer
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2022-04-13T10:46:53.983+00:00

    MainWindow.xaml:

      <StackPanel>  
            <TextBox x:Name="tb" Width="800" Height="100" Background="AliceBlue" />  
            <Button Click="Button_Click" Content="click"/>  
        </StackPanel>  
    

    MainWindow.xaml.cs:

    public partial class MainWindow : Window  
        {  
            string path = @"C:\Users\...\WpfApp2\a.txt";  
            public MainWindow()  
            {  
                InitializeComponent();  
    
            }  
            private void Button_Click(object sender, RoutedEventArgs e)  
            {  
                if (File.Exists(path))  
                {  
                    string projectfolder = "c:\\users\\";  
                    string projectname = "\\0331\\FileOverWriteDemo";  
                    string createText = projectfolder + @"\" + projectname + ".idea";  
                    File.WriteAllText(path, String.Empty);  
                    TextWriter tw = new StreamWriter(path, true);  
                    tw.WriteLine(createText);  
                    tw.Close();  
                    tb.Text = new StreamReader(path).ReadToEnd();  
                }  
            }  
        }  
    

    The result:
    192620-image.png
    192694-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
     Note: Please follow the steps in our [documentation][5] to enable e-mail notifications if you want to receive the related email notification for this thread. 

    [5]: https://learn.microsoft.com/en-us/answers/articles/67444/email-notifications.html

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.