Windows Develop Bookmark and Share   
 index > ClickOnce and Setup & Deployment Projects > Can't create text file output from C# Windows Forms app when deployed with Windows Installer
 

Can't create text file output from C# Windows Forms app when deployed with Windows Installer

Hello,

I'm having trouble trying to deploy my Windows Forms project using the Windows Installer (.msi file output). I built a simple application (C# using VS2005 running on Windows Vista) which consists of a main form with a single button, and a StreamWriter object used to create a text file in the application's startup folder. When the user clicks the button, ten integer values are written to a text file. 

The application works properly when a) debugging out of Visual Studio or b) when I run the executable that gets generated by the normal build process. However, I added a Windows Installer project to the solution and specified that the primary output of my simple application should be the basis of the installation. Now, when I build and run the .msi to install the app and run it, the installed executable seems to run properly, except it doesn't generate the output file. It also does not generate any exceptions or any of the message boxes which I put into catch blocks to try to diagnose the problem.


Below is the code block from my Form1.cs file.

Thank you for looking,
Bob


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace DeploymentTester
{
    public partial class Form1 : Form
    {
        public StreamWriter SW;

        public Form1()
        {
            InitializeComponent();

            string fname = Application.StartupPath + "\\logfile.txt";
            try
            {
                SW = new StreamWriter(fname);
                SW.AutoFlush = true;
                MessageBox.Show("Opened " + fname + " for writing.");
            }
            catch
            {
                MessageBox.Show("Could not open " + fname);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Random rnd = new Random();
            int writeVal;

            try
            {
                button1.Enabled = false;
                System.Threading.Thread.Sleep(100);
                for (int i = 0; i < 10; i++)
                {
                    writeVal = rnd.Next(100);
                    SW.WriteLine(writeVal.ToString());
                }
                button1.Enabled = true;
            }
            catch 
            {
                MessageBox.Show("Could not write file data");
            }
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            SW.Dispose();
        }
    }
}
BobGampert  7 hours 20 minutes ago
Vista runs with UAC, and VS 2005 setup projects don't handle that very well (VS 2008 is better). I suspect your code is running as limited user on Vista (like everything that isn't explicitly elevated) and file virtualization is sending it somewhere else. If you run the MSI from an elevated prompt does it work?
Scroll down to the the file virtualization section:
http://en.wikipedia.org/wiki/User_Account_Control 


Phil Wilson
PhilWilson  4 hours 34 minutes ago

You can use google to search for other answers

Custom Search

More Threads

• EnableVisualStyles() within NTD app
• Deployment File Type settings not overwriting existing association
• ClickOnce - Update
• Updating database using ClickOnce
• Run prerequisites directly from CD
• file save as macro
• Clickonce XNA and Vista
• ClickOnce, Application Settings and Configurations
• Condition Property of CustomAction
• Mage/MageUI or MSBuild.