Trouble with BackGround - c#

I've problems with an image, I've create a background for my application called gridBg.png and I've read it in this way:
string currentDir = Directory.GetCurrentDirectory();
if (File.Exists((currentDir + #"/Images/gridBg.png")))
{
bgAnimated.StopAnimation();
bgAnimated.GifSource = currentDir + #"/Images/gridBg.png";
bgAnimated.NormalLoopFrameCount = 20;
bgAnimated.SpecialLoopFrameCount = 20;
bgAnimated.TotalLoopFrameCount = 40;
bgAnimated.NormalLoopRepeatCount = 1;
bgAnimated.SpecialLoopRepeatCount = 1;
bgAnimated.StartAnimation();
}
On debug mode it works all properly. I've add the image on the setup project and I've given to it Images path. When I install and try the application it works properly too but the problem is that some of my friends does not see the image but the image is on the right place, the Images folder. Anyone has suggests?
SOLVED: string currentDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

The only way I can even think this could happen is the Working Directory for your friends isn't set to the install directory. This would cause the currentDir to be wrong.
Per the MSDN documentation, GetCurrentDirectory does this:
Gets the current working directory of the application

Related

C# Directory.GetCurrentDirectory()

I have windows form app with the following part of code when the Form Loads
public MonitorMail()
{
InitializeComponent();
pathfile = Directory.GetCurrentDirectory();
pathfile = pathfile + #"\Log\Configuration.txt";
var Lista = LoadConfigFile.LoadConfig(pathfile);
if (Lista.Count > 0)
{
SwithMailText.Text = Lista[0];
Excel_Textbox.Text = Lista[1];
LogFileText.Text = Lista[2];
MailServerText.Text = Lista[3];
FromText.Text = Lista[4];
SslText.Text = Lista[5];
UserText.Text = Lista[6];
}
}
As you can see in this code i declare a List named as "Lista" which List takes the records of the Configuration file and fill some textboxes with the data of that Configuration file.
My problem is the following: when I run my program inside in Visual Studio, it loads the records correctly in those textboxes.
When I run my program runs outside of Visual Studio, it also loads the records correctly
BUT
When I try run my program from the command prompt (because this how it should be run) like MonitorMail.exe the program runs but does not show the data in the textboxes.
After trying to understand why is this happening I noticed that is has something to do with
pathfile = Directory.GetCurrentDirectory();
I concluded to that because I changed the pathfile to pathfile="complete path of the Configuration.txt" so when I hit it from cmd works as it should be.
Any idea why Directory.GetCurrentDirectory(); affects cmd? Or is something am I missing?
You wrote in the comments: "i need for every PC to get current directory that my .exe is", but that is not what Directory.GetCurrentDirectory() does...
You need
string myPath = System.Reflection.Assembly.GetEntryAssembly().Location;
instead. That gives you the full path including the file name. You can take the Location's Directory if that is what you need.

Deploy addin project with ppt slide within solution and include it in deployed folder

I am trying to deploy my project having power point slide in solution exlorer. however after I build these it gives me an error as it does not find this ##.ppt file in bin debug. I used build action as content and copy to output directory as copy always but seems of no help. is there any other option for this
Below is my code where I am trying to copy shapes from ppt saved in solution explorer to current ppt now after i deploy since this ppt doesnt add with deployed files it cannot find ppt so it doesnt add shapes.
PowerPoint.Application ppApp = Globals.ThisAddIn.Application;
String programfilesPath = AppDomain.CurrentDomain.BaseDirectory;
//var filesPath = Directory.GetParent(Directory.GetParent(Directory.GetParent(programfilesPath).ToString()).ToString());
String fullPath = programfilesPath;
string pptname = "Moons.ppt";
String themePresentationPath = fullPath + pptname;
// PowerPoint.Application ppapp2 =
var temporaryPresentation = ppApp.Presentations.Open(themePresentationPath, MsoTriState.msoFalse, MsoTriState.msoTrue, MsoTriState.msoTrue);
PowerPoint.SlideRange ppslr = ppApp.ActiveWindow.Selection.SlideRange;
int countlargest = ppslr.Shapes.Count;
string shapecount = harveyballs.SelectedItem.Label.ToString();
int count = Convert.ToInt32(shapecount);
ppslr.Shapes.SelectAll();
PowerPoint.ShapeRange ppShR = ppApp.ActiveWindow.Selection.ShapeRange;
ppShR[count + 1].Copy();
temporaryPresentation.Close();
PowerPoint.SlideRange ppslr2 = ppApp.ActiveWindow.Selection.SlideRange;
ppslr2.Shapes.Paste();
it does not find this ##.ppt file in bin debug
The debug folder is used as an output when current configuration is set to Debug. Are you sure that you have the Debug configuration set in Visual Studio?

Get Folder Path

I want to get the path of the folder to a textbox and the name of the folder is Images12345. I tried this.
//Inside the folder "Images12345"
string[] pics = { "1.jpg", "2.jpg", "3.jpg", "4.jpg", "5.jpg", "6.jpg", "7.jpg", "8.jpg" };
int i = 0;
Then in my form load
//I tried this but it is given me wrong path
textBox1.Text = Path.GetFullPath(#"Images12345");
//then slideshow
pictureBox1.Image = Image.FromFile(textBox1.Text+"/"+pics[0]);
timer1.Enabled = true;
timer1.Interval = 5000;
i = 0;
First, use this to get the application folder plus your images folder:
string applicationPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Images12345");
Second, always use Path.Combine to work with paths:
pictureBox1.Image = Image.FromFile(Path.Combine(myFolderPath, pics[0]));
NOTE
You'll need to copy images folder where the executable is. That's your bin/Degub while you're debugging. You can navigate two folders up, but you must to implement this like you were in production, when the executable will be right next to your image folder.
EDIT
Maybe it's a better approach to use the current user's pictures folder. Like this:
string userPicturesFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
string imagesFolder = Path.Combine(userPicturesFolderPath, "Images12345");
You already found Path.GetFullPath... you should also look at Path.Combine to make a path out of multiple pieces, instead of just using string concatenation.

Loading image from code using relative path in Windows Forms

I have an Image in my application and I have a picture in my WinForms.
public static string Correct_Icons = #"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Correct.png";
public static string warning_Icon = #"C:\Users\xyz\Documents\Visual Studio 2008\Projects\FileShareMgmt\FileShareMgmt\Resources\Warning.png";
cell.Value = Image.FromFile("Resources/warning_Icon);
But I just want to use a relative path and not the full path like above.
For example something like this:
public static string Correct_Icons = "\Resources\Correct.png";
and cont.
..../
not working. Any suggestions?
For my program, Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location) returns
C:\code\test\Junk\bin\Debug.
cell.Value = Image.FromFile(
Path.Combine (
Path.GetDirectoryName (Assembly.GetExecutingAssembly().Location),
"Resources/warning_Icon"));
Of course, usually you would embed the resources in your assembly unless you want to change them without a recompile.
My issue was solved after this solution:
string[] s = { "\\bin" };
string path = Application.StartupPath.Split(s, StringSplitOptions.None)[0] + "\\Images\\On24.png";

C# Windows Application - Linking a button to start a game

I believe I have been taking the right approach to this so far, but I would like to have a button start a video game on my computer.
So far, I have a button linking to a process:
void button2_Click(object sender, EventArgs e)
{
process1.Start();
}
this.process1.EnableRaisingEvents = true;
this.process1.StartInfo.Domain = "";
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe";
this.process1.StartInfo.LoadUserProfile = false;
this.process1.StartInfo.Password = null;
this.process1.StartInfo.StandardErrorEncoding = null;
this.process1.StartInfo.StandardOutputEncoding = null;
this.process1.StartInfo.UserName = "";
this.process1.SynchronizingObject = this;
this.process1.Exited += new System.EventHandler(this.Process1Exited);
So, where-ever I place the EXE (the one I'm coding), it will launch the "marbleblast.exe" under the subfolder MBO relative to it's location.
It seems to be working and trying to launch the game, however, it says it cannot load files that are there. I tested the game without my launcher, and it worked. I believe it's trying to run the EXE, but not letting it use the other files inside of it's folder.
I'll give more details if needed.
How can I get the game to run normally?
try adding this
this.process1.StartInfo.WorkingDirectory= "MBO\\";
or something similar to set the Working Directory.
this.process1.StartInfo.WorkingDirectory= "MBO\";
There's sloppy programming in the game, it relies on the Environment.CurrentDirectory being set right. Which by default is the same directory as where the EXE is located. The upvoted answer repeats the mistake though. To make that statement actually fix the problem, you now rely on your CurrentDirectory being set right. If it is not set where you think it is then it still won't work.
The problem with the program's current directory is that it can be changed by software that you don't control. The classic example is OpenFileDialog with the RestoreDirectory property set to the default value of false. Etcetera.
Always program defensively and pass the full path name of files and directories. Like c:\mumble\foo.ext. To get that going, start with Assembly.GetEntryAssembly().Location, that's the path to your EXE. Then use the System.IO.Path class to generate path names from that. The correct always-works code is:
using System.IO;
using System.Reflection;
...
string myDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string gameDir = Path.Combine(myDir, "MBO");
string gameExe = Path.Combine(gameDir, "marbleblast.exe");
process1.StartInfo.FileName = gameExe;
process1.StartInfo.WorkingDirectory = gameDir;
process1.SynchronizingObject = this;
process1.EnableRaisingEvents = true;
process1.Exited += new EventHandler(Process1Exited);
Set the WorkingDirectory property of the ProcessInfo to the correct directory.
I am Dobrakmato from MBForums. You simple need to add Working directory for Marble Blast.
this.process1.EnableRaisingEvents = true;
this.process1.StartInfo.Domain = "";
this.process1.StartInfo.FileName = "MBO\\marbleblast.exe";
this.process1.StartInfo.WorkingDirectory = "pathto marbleblast.exe directory";
this.process1.StartInfo.LoadUserProfile = false;
this.process1.StartInfo.Password = null;
this.process1.StartInfo.StandardErrorEncoding = null;
this.process1.StartInfo.StandardOutputEncoding = null;
this.process1.StartInfo.UserName = "";
this.process1.SynchronizingObject = this;
this.process1.Exited += new System.EventHandler(this.Process1Exited);

Categories