i did research about this, but nothing worked.
I created folder in my project in Visual Studio 2019 => Images and i placed a file footer.png here.
In Visual Studio => Right click on folder Images, added new item Resources and add this line => Name -> FooterBG and Value -> Images/footer.png
in code:
string pathToFooter = Resource.FooterBG;
in debug get return Images\footer.png
When i want to add this to string with html tags, dont work:
String htmlTags = "<table background='"+ pathToFooter + "'><tbody><tr><td></td></tr></tbody></table>";
Please any tips what im doing wrong ?
Based on my test, the pathToFooter is not the actual path of the image.
Here is a code example you can refer to:
private void button1_Click(object sender, EventArgs e)
{
string pathToFooter = Resource1.FooterBG;
string path = AppDomain.CurrentDomain.BaseDirectory + Path.Combine(#"../..", pathToFooter);
String htmlTags = "<table background='" + path + "' width='1000' height='800' align='center'><tbody><tr><td></td></tr></tbody></table>";
webBrowser1.DocumentText = htmlTags;
}
Result:
Related
As an unexperienced developer I have no idea whether this question is phrased properly (I did check the internet but I havent found a solution that worked for me that is why I signed up here). I am trying to make a form where users can upload files to attach to their form (in SharePoint 2013) and I used the following code as an example. The idea is to temporary accept the files and display them to the user and upload them to the document library when the form is submitted.
In my code however, this results in "acces denied" and when I debugged it, the following piece of my code seemed to be causing the problem:
public void BtnAttachmentUpload_Click(object sender, EventArgs e)
{
fileName = System.IO.Path.GetFileName(FileUpload.PostedFile.FileName);
if (fileName != "")
{
string _fileTime = DateTime.Now.ToFileTime().ToString();
string _fileorgPath = System.IO.Path.GetFullPath(FileUpload.PostedFile.FileName);
string _newfilePath = _fileTime + "~" + fileName;
length = (FileUpload.PostedFile.InputStream.Length) / 1024;
string tempFolder = Environment.GetEnvironmentVariable("TEMP");
string _filepath = tempFolder + "\\" + _newfilePath;
FileUpload.PostedFile.SaveAs(_filepath);
AddRow(fileName, _filepath, DocNo, true);
DocNo = DocNo + 1;
Label.Text = "Successfully added in list";
}
}
The last line of the first section(FileUpload.PostedFile.SaveAs(_filepath);) is where it gives the following error:
"System.UnauthorizedAccessException: 'Access to the path
'C:\Users\Spapps\AppData\Local\Temp\131613662837501509~testdoc2.pdf'
is denied.' "
Is this a known issue and is there a solution that can help me out?
Try with spsecurity.runwithelevatedprivileges
I've been looking to close this question but didnt find how to so Ill state it here as an answer. I havent found a solution, but I worked around this issue in my project by altering the approach; I do not temporarily upload and display the files anymore. They are deleted from the doclib if the procedure is cancelled.
I am very new to C#. I am doing some self studies. I did one small CRUD App using Windows Form Application. It's working fine. Now I want to do one like that in WPF. I realized, some functions, methods are not same as WFA.
Now I want to know how to save the uploaded image to a custom folder.
I have created folder in my Solution called Uploaded. I know how to Upload, Resize the images. Now I want to save this resized image to that Cutome Folder.
Here is my event.
private void SaveBtn_Click(object sender, RoutedEventArgs e)
{
string imagepath = ProfilePicURL.Text;
string picname = imagepath.Substring(imagepath.LastIndexOf('\\'));
//Rename the file as per the user first and last name
picname = FirstName.Text.Replace(" ", String.Empty) + "_" + LastName.Text.Replace(" ", String.Empty);
}
string imagepath = ProfilePicURL.Text;
var imageFile = new System.IO.FileInfo(imagepath);
if (imageFile.Exists)// check image file exist
{
// get your application folder
var applicationPath=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
// get your 'Uploaded' folder
var dir=new System.IO.DirectoryInfo(System.IO.Path.Combine(applicationPath,"uploaded"));
if(!dir.Exists)
dir.Create();
// Copy file to your folder
imageFile.CopyTo(System.IO.Path.Combine(dir.FullName,string.Format("{0}_{1}",
FirstName.Text.Replace(" ", String.Empty),
LastName.Text.Replace(" ", String.Empty))));
}
I have opened Resources.resx file of a WinForms application project and copied images there. I am using the code shown below to get an image from the resources, but getting following error:
Value of 'null' is not valid for 'stream'.
The error occurs in this line:
btn.BackgroundImage = new Bitmap(
System.Reflection.Assembly.GetEntryAssembly().GetManifestResourceStream(test));
Relevant code:
private void genericButton_event(object sender, EventArgs e)
{
var btn = (Button)sender;
string test = "StudentModule.Properties.Resources" + btn.Name + ".png";
//Getting the error here:
btn.BackgroundImage = new Bitmap(System
.Reflection
.Assembly
.GetEntryAssembly()
.GetManifestResourceStream(test));
}
The value of test is "StudentModule.Properties.ResourcesbtnAbout.png", but I think it should be: "StudentModule.Properties.Resources.btnAbout.png". I tried this line also but it's not working:
string test = "StudentModule.Properties.Resources." + btn.Name + ".png";
What is the mistake I am doing here?
GetManifestResourceStream() returns null when the stream could not be found. Which will then bomb the Bitmap constructor.
So the string you used is wrong. It isn't clear exactly how you embedded the resource. Do strongly favor using the resource designer, Project + Resources tab. Click the arrow on the Add Resource button, choose "Add Existing File" and select the file. You can then use the ResourceManager to get the bitmap:
var imageName = "Chrysanthemum";
btn.BackgroundImage = (Bitmap)Properties.Resources.ResourceManager.GetObject(imageName);
Note how the resource name is just the plain resource name as it appears in the resource designer.
Okay so i am fairly new to programming and was told to just start acouple projects and google/ask if i need hep so here i am, im making a program that will download and run a file from the internet and when i run the release app i get this error http://gyazo.com/7b017f0ed550af3b86b24ad480db8fe8 so i press the details and it give me this long error report thing http://pastebin.com/Ag7u9gBZ i dont seem to have enough knowledge to be able to read and debug it, and i am very confussed, from what i see it has to do with system.io and webclient and path or something i dont know.
private void startBotToolStripMenuItem_Click(object sender, EventArgs e)
{
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21FB6AD2A0A1EB413347302A46C5FB1A39599DF740D6&directstart=1";
WebClient wc = new WebClient();
wc.DownloadFile(direct_exe_from_url, filepath);
Process.Start(filepath);
}
You're getting a System.IO.DirectoryNotFoundException so I would guess that this: C:\Users\Owner\AppData\Roaming\Downloaded Files\ directory does not exist.
You can use the Directory.CreateDirectory method to create the directory before putting the downloaded file in there, e.g:
string directoryPath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\";
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21
//Rest of your code
I want to read the first line of a text file that I added to the root directory of my project. Meaning, my solution explorer is showing the .txt file along side my .cs files in my project.
So, I tried to do:
TextReader tr = new StreamReader(#"myfile.txt");
string myText = tr.ReadLine();
But this doesn't work since it's referring to the Bin Folder and my file isn't in there... How can I make this work? :/
Thanks
From Solution Explorer, right click on myfile.txt and choose "Properties"
From there, set the Build Action to content
and Copy to Output Directory to either Copy always or Copy if newer
You can use the following to get the root directory of a website project:
String FilePath;
FilePath = Server.MapPath("/MyWebSite");
Or you can get the base directory like so:
AppDomain.CurrentDomain.BaseDirectory
Add a Resource File to your project (Right Click Project->Properties->Resources). Where it says "strings", you can switch to be "files". Choose "Add Resource" and select your file.
You can now reference your file through the Properties.Resources collection.
private string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
The method above will bring you something like this:
"C:\Users\myuser\Documents\Visual Studio 2015\Projects\myProjectNamespace\bin\Debug"
From here you can navigate backwards using System.IO.Directory.GetParent:
_filePath = Directory.GetParent(_filePath).FullName;
1 time will get you to \bin, 2 times will get you to \myProjectNamespace, so it would be like this:
_filePath = Directory.GetParent(Directory.GetParent(_filePath).FullName).FullName;
Well, now you have something like "C:\Users\myuser\Documents\Visual Studio 2015\Projects\myProjectNamespace", so just attach the final path to your fileName, for example:
_filePath += #"\myfile.txt";
TextReader tr = new StreamReader(_filePath);
Hope it helps.
You can have it embedded (build action set to Resource) as well, this is how to retrieve it from there:
private static UnmanagedMemoryStream GetResourceStream(string resName)
{
var assembly = Assembly.GetExecutingAssembly();
var strResources = assembly.GetName().Name + ".g.resources";
var rStream = assembly.GetManifestResourceStream(strResources);
var resourceReader = new ResourceReader(rStream);
var items = resourceReader.OfType<DictionaryEntry>();
var stream = items.First(x => (x.Key as string) == resName.ToLower()).Value;
return (UnmanagedMemoryStream)stream;
}
private void Button1_Click(object sender, RoutedEventArgs e)
{
string resName = "Test.txt";
var file = GetResourceStream(resName);
using (var reader = new StreamReader(file))
{
var line = reader.ReadLine();
MessageBox.Show(line);
}
}
(Some code taken from this answer by Charles)
You have to use absolute path in this case. But if you set the CopyToOutputDirectory = CopyAlways, it will work as you are doing it.
In this code you access to root directory project:
string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
then:
StreamReader r = new StreamReader(_filePath + "/cities2.json"))