0

I creted an application that has ini file and db.mdb (access) and it downloads some images from web to a folder that is near of App.

Well, I created a setup file with Setup Factory Software. Everything seems quite fine and I started my application and it doesnt change ini file's value, doesnt download images and doesnt insert any recort to db.

here is the path of my app , C:\Program Files (x86)\XXXXXXX

here is code of instering row to db

public void AddChannels(List<MediaChannel> list)
{

 string connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=data\\db.mdb;";
 try
 {
  using (OleDbConnection connection = new OleDbConnection(connectionString))
  {
   OleDbCommand cmd = new OleDbCommand();
   cmd.Connection = connection;

   connection.Open();


   int tmp1;
   string tmpstr;
   foreach (var mediaChannel in list)
   {
    tmp1 = mediaChannel.ImagePath.LastIndexOf('/');
    tmpstr = mediaChannel.ImagePath.Substring(tmp1+ 1, mediaChannel.ImagePath.Length - tmp1 - 1);

    cmd.CommandText = "Insert Into Channels(ChannelName,CategoryName,Url,ImagePath,ChannelType) values(@ChannelName,@CategoryName,@Url,@ImagePath,@ChannelType)";
       cmd.Parameters.AddWithValue("@ChannelName", mediaChannel.Name);
       cmd.Parameters.AddWithValue("@CategoryName", mediaChannel.CategoryName);
       cmd.Parameters.AddWithValue("@Url", mediaChannel.Url);
       cmd.Parameters.AddWithValue("@ImagePath", tmpstr);
       cmd.Parameters.AddWithValue("@ChannelType", (int)mediaChannel.ChannelType);
       cmd.ExecuteNonQuery();
      cmd.Parameters.Clear();
   }
  connection.Close();
 }
} 
catch (Exception ex)
{

throw new Exception(ex.Message);
}
}

and here is my image downloading method

private void DownloadLogo()
{

string localFilename = Application.StartupPath + @"\Imgs\";
if (!Directory.Exists(localFilename))
Directory.CreateDirectory(localFilename);

foreach (var mediaChannel in channelList)
{

string imgName = mediaChannel.ImagePath;
if (imgName.Contains("http://"))
{
int tmp1 = mediaChannel.ImagePath.LastIndexOf('/');
imgName = mediaChannel.ImagePath.Substring(tmp1 + 1, mediaChannel.ImagePath.Length - tmp1 - 1);

}


if (!File.Exists(localFilename + imgName))
{


using (var client = new WebClient())
{

client.DownloadFile(mediaChannel.ImagePath, localFilename + imgName);
}
}
}
}

Everything looks OK and I have never get this kind of issue. If I run my program as administrator, it works...

One more thing, I opened my db that is in C:\Program Files (x86)\XXXXXXX\data I used MS Access and interesting, I can not edit table. It says me you need to save as your file for changing.. I think program files (86) is protected by UAC. My OS is Windows 8 Professional.

Maybe setup program makes it like that? Because I cant use visual studio setup because of limited license of install shield setup then I used 3rd part setup maker app.

So, How to solve this problem? My customer is waiting for me to fix this issue..

ertan2002
  • 1,458
  • 1
  • 32
  • 68

3 Answers3

4

You probably need to use AppData folder instead of Program Files folder. Check this similar question Saving a file to Application Data in c#

Community
  • 1
  • 1
Radenko Zec
  • 7,659
  • 6
  • 35
  • 39
  • Thank you Radenko for your answer that is what I am looking for. As Matthew said, to share the db among all users, ProgramData looks better, right? Is there any required like UAC? – ertan2002 Jan 14 '14 at 13:49
  • The AppData folder is specific to each user, so only use that if you want a separate database file for each user. If you want to share the database file between all users, you need to use ProgramData instead. – Matthew Watson Jan 14 '14 at 13:49
  • 1
    @ertan2002 You shouldn't need UAC to access folders underneath ProgramData. – Matthew Watson Jan 14 '14 at 13:50
  • thank you @MatthewWatson, I will use this folder, but I do not have any idea whether XP users have it or not, I hope that it will not problem for them :)) – ertan2002 Jan 14 '14 at 13:53
  • 1
    @ertan2002: You should use this to get the base folder: `string dataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);` (It should get the right path under XP and Windows 7 or 8.) Then create an app-specific folder underneath that and install the database file in it. – Matthew Watson Jan 14 '14 at 13:55
  • thank you for your answer, its really useful information. I thought that ProgramData folder is just special for Win 7& 8 OS. I understood what I need to do for coding and now I am looking for how to install db to this folder visa Setup Factory. For Program files, It says %AppFolder%, is there any special defination for programdata like %ProgramData% ,do you have any idea for it? (its my last question :)) ) – ertan2002 Jan 14 '14 at 13:58
  • There is @ertan2002 There's an environment variable on Vista or later called ProgramData which points to the "Program Data" folder - but this environment variable doesn't exist on XP afaiaa. On XP there's an env var called "ALLUSERSPROFILE" which points to "C:\Documents and Settings\All Users" – Matthew Watson Jan 14 '14 at 14:52
  • thank you for your all attention. But I solved my own problem. Its not about program files folder or programdata etc. Setup maker hadnt gave permssion this files thats why I cant write over files. Now everything is OK – ertan2002 Jan 14 '14 at 14:54
3

You're trying to modify a file within Program Files. That's an area of the file system which isn't designed for mutable application data - it's basically meant to be read-only. Modifications to this area are suspect, hence the UAC prompt.

You should fix this by storing your data in a more appropriate place in the file system. Exactly where that is will depend on what your application is trying to do. (Is it a service, or just a local client app? Is the data meant to be specific to the logged-in user, or computer-wide?)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

Finally I solved what was the problem.. I gave my files permissions to write and everything is OK.

http://www.indigorose.com/webhelp/suf9/Program_Reference/Actions/File.SetPermissions.htm

ertan2002
  • 1,458
  • 1
  • 32
  • 68