Showing Image From a specific directory - c#

I have this project wherein I need to load pictures from a specific directory.
This code is working:
protected void Button1_Click(object sender, EventArgs e)
{
ShoImages(Server.MapPath(#"~/Images/"), "Images/");
}
Loads perfectly if I'm loading images of "Images" Folder inside my Visual Studio Project, but I want to load images inside a directory I'm getting error using this code below:
protected void Button1_Click(object sender, EventArgs e)
{
string strpath = #"D:\New Project\Uploads\12345\";
ShoImages(Server.MapPath(strpath), strpath);
}
The Error is - "'D:\New Project\Uploads\12345\' is a physical path, but a virtual path was expected."
Can you Please help me and give me an idea what to do. New with mappath. Thank you

MapPath takes a relative URL (~/Images/) and changes it into a local path on your machine (D:\Images). If you have a local path already, you don't need to call MapPath:
protected void Button1_Click(object sender, EventArgs e)
{
string strpath = #"D:\New Project\Uploads\12345\";
ShoImages(strpath, strpath);
}

Remove server.map path from ur code
ShoImages(strpath, strpath);

I used this :
ShoImages(HttpContext.Current.Server.MapPath(#"~/Uploads/12345/"), "Uploads/12345/");
instead HttpContext.Current.Server.MapPath pointing to my directory.
Thanks for sharing your ideas. Appreciated it. :D

Related

File.WriteAllText() statement in C# not creating a file

I am still only just learning C# in Visual Studio and I am trying to make a simple text encryption application. My problem at the moment is that when I use the command:
File.WriteAllText(name, inputTextBox.Text);
(Where name is the name of the file chosen in a SaveFileDialog and inputTextBox.Text is the text in a textbox on the main form)
however the file is never actually created. I even tried to build the application and run it as administrator but nothing happened.
What's even weirder is when I opened File Explorer, in the Quick Access section where it shows recent files, all the files that were supposed to be created show up there but don't exist when I click "Open File Location" and if I just try to open them, notepad just tells me the file doesn't exist.
The files are also not in my recycle bin or anything.
Here's the rest of my code in case it's something wrong with that:
public Form1()
{
InitializeComponent();
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
And in case you're wondering saveDialog is already an element in my form so there's no problem with that.
Since in your posted code the initialization of the SaveFileDialog is missing, and you say in your comment that the Debugger doesn't halt in the event body I take the long shot to assume that the event is not registered appropriately.
Try to make sure that your class (minimally) looks like the following example:
public partial class Form1 : Form
{
SaveFileDialog saveDialog;
public Form1()
{
InitializeComponent();
// create instance of SaveFileDialog
saveDialog = new SaveFileDialog();
// registration of the event
saveDialog.FileOk += SaveDialog_FileOk;
}
private void saveButton_Click(object sender, EventArgs e)
{
saveDialog.ShowDialog();
}
private void saveDialog_FileOk(object sender, CancelEventArgs e)
{
string name = saveDialog.FileName;
File.WriteAllText(name, inputTextBox.Text);
}
}
If your problem still remains, then I will remove my answer

C# load video from resources folder not playing

I trying to load my video from the resources folder but its not playing automatically once my form load. Can I know what mistake i have done. Thank you.
This is my c# code:
private void Form2_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"Resources/abc.mp4";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
Well I have solved it my ownself. Actually, I accidentally added the # symbol into my url. That causes the problem. This is the updated code.
private void Form2_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = "Resources\\abc.mp4";
axWindowsMediaPlayer1.Ctlcontrols.play();
}
To do that king of thing, you must get the stream of your resource.
So that code should work for you because works for me :)
// temporary file path - your temp file = video.avi
var strTempFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Video.avi");
try
{
// ResourceName = the resource you want to play
File.WriteAllBytes(strTempFile, Properties.Resources.ResourceName);
axWMP.URL = strTempFile;
axWMP.Ctlcontrols.play();
}
catch (Exception ex)
{
// Manage me
}
You can implement a axWMP_PlayStateChange method to remove video.Avi at the end.
Hope it could help you

File Upload path needed

Hi I am trying to save only the path of a file using fileupload . On clicking the button I want only the complete path that the user has selected into the file upload to be stored into the database. Just for testing purposes I am using labels in the code below but ultimately I will connect it to a data base.I only need to store the path selected by the user and not the file.
HTML
C# that I have been trying but not working
protected void Button1_Click(object sender, EventArgs e)
{
string g = FileUpload1.FileName;
string b =Convert.ToString(FileUpload1.PostedFile.InputStream);
//string filepath = Path.GetFullPath(FileUpload1.FileName.toString());
Label1.Text = g;
Label2.Text =b;
}
change your code as below:
protected void Button1_Click(object sender, EventArgs e)
{
string g = Server.MapPath(FileUpload1.FileName);
string b =Convert.ToString(FileUpload1.PostedFile.InputStream);
//string filepath = Path.GetFullPath(FileUpload1.FileName.toString());
Label1.Text = g;
Label2.Text =b;
}
You can't.
See also here: How can I get file.path in plupload?
If I get you correctly, this is some kind of intranet application?
If this needs to work within a closed domain, you might as well think about a desktop application instead.
You need a path for save file? Try this:
this.MapPath("~")
or
this.yourUploadFile.PostedFile.SaveAs(this.MapPath("~") + "YOUR FOLDER + NAME");

How to open notepad in c# by button

I am new to programming and stuck on a little thing. I have a button on my Windows application and I want to open Notepad when I click the button. I used all the available codes from internet starting from process.start() to even envirnoment.path but the button doesn't show the Notepad. Here is what I have already tried.
private void btnNotepad_Click(object sender, EventArgs e)
{
string notepadPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "notepad.exe");
System.Diagnostics.Process.Start(notepadPath);
}
Or simply:
system.diagnostics.process.start(#"notepad.exe");
Also did this:
string theData = txtbxRepeat.Text;
FileStream aFile = new FileStream("myTextFile.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
txtbxRepeat.Text = theData;
sw.WriteLine(theData);
sw.Close();
Please help me in this.
You are heading in the correct direction with the first and 2nd code snippet. However, you need to specify the full path to the notepad++ exe.
private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86), "Notepad++", #"notepad++.exe"));
}
However, keep in mind the user may have installed notepad++ in a different directory (for example they don't have an x86 directory).
UPDATED: updated to include environment paths rather than hard-coded path.

Having trouble creating a new folder on user input Visual Studio 2010 C#

Essentially I have a button and a text box and when the user inputs text and hits the button i want it to create anew folder in a selected destination, ive got my code currently and cant figure out why it wont work
private void button1_Click(object sender, EventArgs e)
{
if (!Directory.Exists("C:\\Users\\Ben\\Documents\\CreateDirectoryTest" + Searchbox.Text))
{
Directory.CreateDirectory("C:\\Users\\Ben\\Documents\\CreateDirectoryTest" + Searchbox.Text);
}
}
am i missing something? help would be really appreciated
Don't concatenate file system path's manually. Use the methods of System.IO:
private void button1_Click(object sender, EventArgs e)
{
const string path = "C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\";
var directory = System.IO.Path.Combine(path, Searchbox.Text);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
I assume you're trying to check for a subdirectory of CreateDirectoryTest and create a directory inside of it if not. The way you're concatenating the string, if Searchbox.text is "TheFolder" for example, your string would end up looking like this:
C:\Users\Ben\Documents\CreateDirectoryTestTheFolder
You can either add a \\
if (!Directory.Exists("C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\" + Searchbox.Text))
{
Directory.CreateDirectory("C:\\Users\\Ben\\Documents\\CreateDirectoryTest\\" + Searchbox.Text);
}
Or just use Path.Combine:
string path = System.IO.Path.Combine("C:\\Users\\Ben\\Documents\\CreateDirectoryTest", Searchbox.Text);
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}

Categories